List readonly with a private set

后端 未结 15 2042
北海茫月
北海茫月 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:14

    Why are you using a List. Sounds like what you really want to expose is IEnumerable

    public IEnumerable myList { get; private set; }
    

    Now users of the class can read the items but not chagnge the list.

提交回复
热议问题