How to make IEnumerable readonly?

前端 未结 8 654
臣服心动
臣服心动 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条回答
  •  -上瘾入骨i
    2021-02-07 12:53

    IEnumerable is readonly

    p is a new collection which doesn't depend on list1instance. The mistake you made, is that you thought that this line list[0].FirstName = "uf1";
    would only modify one of the lists, when on fact you're modifying the Person object.
    The two collections are distinct, they just happen to have the same items.
    To prove that they are different, try adding and removing items from one of the lists, and you'll see that the other one isn't affected.

提交回复
热议问题