Skip to content

Can I create my own runtime constant? #46809

Answered by tannergooding
msedi asked this question in Q&A
Discussion options

You must be logged in to vote

static readonly properties are already elided where possible (at least for primitive value types).

That is the following program:

class Program
{
    public static readonly uint DebugLevel = GetAppContextData("DebugLevel", defaultValue: 0);

    private static uint GetAppContextData(string name, uint defaultValue)
    {
        var data = AppContext.GetData(name);

        if (data is uint value)
        {
            return value;
        }
        else if ((data is string stringValue) && uint.TryParse(stringValue, out var result))
        {
            return result;
        }
        else
        {
            return defaultValue;
        }
    }

    public static void Main(string[] args

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by msedi
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants