C# – readonly keyword does not make a List<> ReadOnly

ccollectionslist

I have the following code in a public static class:

public static class MyList
{
    public static readonly SortedList<int, List<myObj>> CharList;
    // ...etc.
}

.. but even using readonly I can still add items to the list from another class:

MyList.CharList[100] = new List<myObj>() { new myObj(30, 30) };

or

MyList.CharList.Add(new List<myObj>() { new myObj(30, 30) });

Is there a way to make the thing read only without changing the implementation of CharList (it'll break some stuff)?
If I do have to change the implementation (to make it non-changeable), what would be the best way?
I need it to be List<T, T>, so ReadOnlyCollection won't do

Best Answer

The modifier readonly means that the value cannot be assigned except in the declaration or constructor. It does not mean that the assigned object becomes immutable.

If you want your object to be immutable, you must use a type that is immutable. The type ReadOnlyCollection<T> that you mentioned is an example of a immutable collection. See this related question for how to achieve the same for dictionaries: