dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.89k stars 4.01k forks source link

constant string allow conversions #74369

Open LeonSchourek opened 1 month ago

LeonSchourek commented 1 month ago

Background and Motivation

I woold like to use numbers in const Strings i could use that for example for writing sql querrys

Proposed API

allow const values to be convertet to const strings

Usage Examples

const double dpi = 2 * 3.14;
const string nerg = $"double of PI is:{dpi}";
const bool debug = false;
const string debugtext = $"debug enabled: " + debug;
public enum LivingType
{
    Human,

    Animal
}

public class LivingObject
{
    public string name {  get; set; }
    public LivingType type { get; set; }
}
const string query = $"""
SELECT
    *
FROM
    {nameof(LivingObject)}DB
WHERE
    {nameof(LivingObject.type)} = {(int)LivingType.Human}
""";

Alternative Designs

Risks

no risks

huoyaoyuan commented 1 month ago

Language requests goes to https://github.com/dotnet/csharplang/discussions .

Note that interpolated strings are culture-aware. See https://github.com/dotnet/csharplang/discussions/177 and https://github.com/dotnet/csharplang/discussions/384 .