Good morning SOes brothers, I have this situation:
I need a dictionary with data previously defined, this dictionary is supposed to be read-only so the million dollar question is:
How can I define a static dictionary with initialized elements?
This way when you have to work some clave
, the valor
will be obtained from the dictionary.
If the data in that dictionary is read-only and it's a dictionary for the whole class, do it
static
and of typereadonly
And you use it as follows:
That is, if in
VariableDeNombreDeAlgunaClave
contains a string namedClave1
, the value will beValor1
.Instead of a Dictionary you use a ReadOnlyDictionary:
Or if you use a version of Visual Studio lower than 2015:
Although I prefer the first option, it is clearer that they are data from a dictionary.