How to make IEnumerable readonly?

前端 未结 8 662
臣服心动
臣服心动 2021-02-07 12:23

Why are the lists list1Instance and p in the Main method of the below code pointing to the same collection?

class Person
         


        
8条回答
  •  情歌与酒
    2021-02-07 12:53

    First of all, your List in your class is public, so there's nothing stopping anyone from directly accessing the list itself.

    Secondly, I would implement IEnumerable and return this in my GetEnumerator Method

    return l1.AsReadOnly().GetEnumerator();
    

提交回复
热议问题