同一类对象的多个引用赋值 - 引用问题
这个演示类解释了这个问题
public class SomeClass
{
public string Name { get; set; }
public int Age { get; set; }
}
在开发与我包含在这个问题中的示例代码相同的东西时,我得到了以下想法:
由于类是引用类型,并且如果我将多个实例分配给同一个类对象
使用循环
并将这些对象存储在列表中,这还不足以破坏每个对象并使其等于分配给它的最后一个实例吗?
这是一些示例实现,用于解决测试后的混乱
List<SomeClass> lst = new List<SomeClass>();
SomeClass someClassObj = null;
for (int i = 0; i < 3; i++)
{
someClassObj = new SomeClass();
someClassObj.Name = "Name " + i.ToString();
someClassObj.Age = i;
lst.Add(someClassObj);
}
,它并不像我想象的那样工作,无论如何我想要的
任何人都可以帮助消除这种混乱。
This demo class to explain the question
public class SomeClass
{
public string Name { get; set; }
public int Age { get; set; }
}
While developing something equals to the sample code
I included in this question I got the following thought:
Since classes are reference type, and if I assign multiple instances to the same class object
using loop
and store these objects in an list, isn't that enough to ruin each object and make it equals to last instance assigned to it?
Here's some sample implementation for the confusion
List<SomeClass> lst = new List<SomeClass>();
SomeClass someClassObj = null;
for (int i = 0; i < 3; i++)
{
someClassObj = new SomeClass();
someClassObj.Name = "Name " + i.ToString();
someClassObj.Age = i;
lst.Add(someClassObj);
}
after testing it does not wokred the way I though it would, anyway that what i want
anyone help to clear this confusion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次执行
someClassObj = new SomeClass();
时,都会在堆中创建一个新的内存块,并将其地址分配给
someClassObj
这意味着在您的列表中实际上不仅有您一次又一次添加的单个地址,但是一个新地址,这就是为什么当您比较对象时它们不会相同,因为它们最初
有不同的地址
SomeClass someClassObj = null;
您的对象是没有指向任何东西。当您使用 new () 创建新实例时,您的对象开始指向新分配的内存地址。因此,在循环内部,每个实例都被分配了新地址,并且该地址存储在相同的指针/引用中的 SomeClass 中。当您在列表中添加项目时,它实际上添加了SomeClass
当前指向的项目的地址/引用Every time you do
someClassObj = new SomeClass();
a new memory piece is created in heap and it's address is assigned to
someClassObj
which means in your list there is not actually only single address which you are adding again and again but a new address and that's why when you will compare the object they won't be same because they have a different addressinitially
SomeClass someClassObj = null;
your object is pointing to nothing. when you create a new instance usingnew ()
your object starts pointing to newly allocated memory's address. So, inside loop, every instance is assigned new address and that address is stored inSomeClass
which is same pointer/reference. When you add item in the list, it actually adds the address/reference of item that is currently pointed bySomeClass
您实际上要做的是在每次迭代时为
someClassObj
分配一个新引用,然后将该引用的副本添加到您的列表中。您没有将对someClassObj
的引用添加到您的列表中。Add
方法需要采用ref
参数才能成为对someClassObj
的引用。What you are actually doing is assigning a new reference to
someClassObj
on each iteration, and then adding a copy of this reference to your list. You are not adding a reference tosomeClassObj
to your list. TheAdd
method would need to take aref
parameter in order for it to be a reference tosomeClassObj
.