How to make IEnumerable readonly?

前端 未结 8 652
臣服心动
臣服心动 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

    You could make a deepclone of each item in the list, and never return references to your original items.

    public IEnumerable Get()
    {
      return l1
        .Select(p => new Person(){
          FirstName = p.FirstName,
          LastName = p.LastName
        });
    }
    

提交回复
热议问题