Home > Enterprise >  C# - Why is it possible for a readonly dictionary to "set"?
C# - Why is it possible for a readonly dictionary to "set"?

Time:09-05

Apologies first for the image. I don't have the source.

My question is why is it possible to "set" a readonly dictionary (last line of the program) ? What's the difference if the dictionary is NOT declared as readonly ?

enter image description here

Thanks.

CodePudding user response:

The reason is that your dictionary is not read only. It is a "regular" dictionary with all its functionality. What is read only is the field _dictionary which holds a reference to the actual Dictionary. Therefore, you cannot replace the dictionary assigned to _dictionary.

CodePudding user response:

Because "readonly" here is field _dictionary itself. But Dictionary class is mutable and you still can change internal values.

  •  Tags:  
  • c#
  • Related