I am just wondering how I could add elements to the Dictionary below even if it is declared as readonly.
Are we not supposed to be able to modify the dictionary if it is declared as readonly?
Thanks.
class program
{
private static readonly Dictionary
static void Main(string[] args)
{
_dictionary.Add("a",1);
_dictionary.Add("b",2);
_dictionary.Add("c",3);
}
}
VulpesPosted Mar 25, 2011, 10:11 AM
What you can't do is to assign a different Dictionary object to it which can only be done from the constructor or 'in situ'.
VulpesPosted Mar 26, 2011, 7:47 AM
It's just a Latin phrase which means "in the original position".
Sam HobbsPosted Mar 26, 2011, 12:43 AM
VulpesPosted Mar 25, 2011, 10:22 AM
So, in your code, you've assigned a new Dictionary object to the readonly field _dictionary when you've declared it or 'in situ'.
As it's a private static field, you can add elements to it (or remove elements from it) from any instance or static method in the program class but you can't change the actual Dictionary object itself.
Tommy SinPosted Mar 25, 2011, 10:17 AM
Thanks for your explanation.
By the way, what do you mean by 'in situ'?
Thanks