C#:将同一个对象添加到两个 List<object> 变量时,过程中是否克隆了对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/957287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: When adding the same object to two List<object> variables, is the object cloned in the process?
提问by Austin Hanson
I have something similar to this:
我有类似的东西:
// Declarations:
List<SomeType> list1 = new List<SomeType>();
List<SomeType> list2 = new List<SomeType>();
...
SomeType something = new SomeType("SomeName");
list1.Add(something);
list2.Add(something);
...
list1[indexOfSomething] = new SomeType("SomeOtherName");
And the object in list2 isn't changed... Is that the expected result?
并且 list2 中的对象没有改变......这是预期的结果吗?
采纳答案by Can Berk Güder
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.
并且对象 inlist2
也会改变。
回答by Joseph
Yes, you're not cloning the object. The object is being added to both lists originally by reference, and then subsequently you're assigned a reference in the list to the new object you're creating.
是的,您不是在克隆对象。该对象最初是通过引用添加到两个列表中的,然后您在列表中分配了一个对您正在创建的新对象的引用。
That is definitely the expected result.
这绝对是预期的结果。
回答by Fredrik M?rk
You are not cloning the object; you are adding a reference to the same object in the two lists. However, your code replaces the reference in one of the lists with a reference to another object, so yes, this is the expected behaviour.
你不是在克隆对象;您正在两个列表中添加对同一对象的引用。但是,您的代码将其中一个列表中的引用替换为对另一个对象的引用,所以是的,这是预期的行为。
回答by JaredPar
Yes that is expected. Only the reference to the object is added. Not the reference itself or a copy.
是的,这是预期的。仅添加对对象的引用。不是参考本身或副本。
回答by Jon Erickson
When you pass the 'something' object to Add you are passing by value (c# default), not by reference
当您将“某物”对象传递给 Add 时,您是按值传递(c# 默认值),而不是按引用传递
回答by Jon B
You're replacing the reference in one list with a reference to a new object. If you were to instead change a property of that object, you would see it changed in both places, since the reference would remain the same.
您正在用对新对象的引用替换一个列表中的引用。如果您改为更改该对象的属性,您会看到它在两个地方都发生了变化,因为引用将保持不变。
回答by Juliet
// Declarations:
List<SomeType> list1 = new List<SomeType>();
List<SomeType> list2 = new List<SomeType>();
...
SomeType something = new SomeType("SomeName");
list1.Add(something);
list2.Add(something);
Remember, when you add an object to a list, you're really just adding a pointer to the object. In this case, list1 and list2 both point to the same address in memory.
请记住,当您将一个对象添加到列表中时,您实际上只是添加了一个指向该对象的指针。在这种情况下,list1 和 list2 都指向内存中的相同地址。
list1[indexOfSomething] = new SomeType("SomeOtherName");
Now you've assigned the element list1 to a different pointer.
现在您已将元素 list1 分配给不同的指针。
You're not really cloning objects themselves, you're copying the pointers which just happen to be pointing at the same object. If you need proof, do the following:
您并不是真正克隆对象本身,而是复制恰好指向同一对象的指针。如果您需要证明,请执行以下操作:
SomeType something = new SomeType("SomeName");
list1.Add(something);
list2.Add(something);
list1[someIndex].SomeProperty = "Kitty";
bool areEqual = list1[someIndex].SomeProperty == list2[someIndex].SomeProperty;
areEqual
should be true. Pointers rock!
areEqual
应该是真的。指针摇滚!