Define dictionary as constant

1

Follow the code below:

var values = new Dictionary<string, string>
{
    { "tipo1", "dada65d" },
    { "tipo2", "546541354765321" },
    { "tipo3", "klwoiqyhalwtbfowh" }
};

How can I declare class Dictionary as constant ( const )?

    
asked by anonymous 21.06.2018 / 01:46

2 answers

2

I left another alternative using readonly

private readonly static Dictionary<string, string> values = new Dictionary<string, string>()
{
    { "tipo1", "dada65d" },
    { "tipo2", "546541354765321" },
    { "tipo3", "klwoiqyhalwtbfowh" }
};
    
21.06.2018 / 02:03
1

For these cases I usually use an enum, because it is, in a certain way, a constant dictionary.

But anyway you can use readonly so that it is written only in the constructor and not in other places.

    
21.06.2018 / 11:15