List readonly with a private set

后端 未结 15 2010
北海茫月
北海茫月 2020-11-30 01:57

How can I expose a List so that it is readonly, but can be set privately?

This doesn\'t work:

public List myList          


        
15条回答
  •  天命终不由人
    2020-11-30 02:02

    If you want readonly collection use ReadOnlyCollection, not List:

    public ReadOnlyCollection MyList { get; private set; }
    

提交回复
热议问题