readonly keyword does not make a List<> ReadOnly?

后端 未结 3 1907
心在旅途
心在旅途 2020-12-01 14:17

I have the following code in a public static class:

public static class MyList
{
    public static readonly SortedList> CharList         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 14:41

    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 that you mentioned is an example of a immutable collection. See this related question for how to achieve the same for dictionaries:

    • Is there a read-only generic dictionary available in .NET?

提交回复
热议问题