List readonly with a private set

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

    I prefer to use IEnumerable

    private readonly List _list = new List();
    
    public IEnumerable Values // Adding is not allowed - only iteration
    {
       get { return _list; }
    }
    

提交回复
热议问题