C#: When adding the same object to two List<object> variables, is the object cloned in the process?

后端 未结 7 1041
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 19:28

I have something similar to this:

// Declarations:
List list1 = new List();
List list2 = new List

        
7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 20:13

    Yes, but nothing's cloned. Before the assignment, the same object is in both lists. After the assignment, you have two unique objects in two lists.

    Do This:

    list1[indexOfSomething].name = "SomeOtherName";
    

    and the object in list2 will change, too.

提交回复
热议问题