How do I change my new list without changing the original list?

后端 未结 11 1873
萌比男神i
萌比男神i 2020-12-03 08:30

I have a list that gets filled in with some data from an operation and I am storing it in the memory cache. Now I want another list which contains some sub data from the li

11条回答
  •  庸人自扰
    2020-12-03 09:06

    Your are seeing the original list being modified because, by default, any non-primitive objects, are passed by reference (It is actually pass by value, the value being the reference, but that is a different matter).

    What you need to do is clone the object. This question will help you with some code to clone a List in C#: How do I clone a generic list in C#?

提交回复
热议问题