What does immutable and readonly mean in C#?

Viewed 12924

Is it correct that it is not possible to change the value of an immutable object?

I have two scenarios regarding readonly that I want to understand:

  1. What if I have a collection and mark it as readonly, like the following. Can I still call _items.Add?

    private readonly ICollection<MyItem> _items;
    
  2. And also for the following variable, if later on I call _metadata.Change which will change the internal values of a couple member variable in the Metadata instance. Is _metadata still immutable?

    private readonly Metadata _metadata;
    

For both variables above, I totally understand that I can't directly assign new values to them outside of initializer and constructors.

8 Answers
Related