同一类对象的多个引用赋值 - 引用问题

发布于 2024-11-29 11:51:15 字数 682 浏览 0 评论 0原文

这个演示类解释了这个问题

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-12-06 11:51:15

每次执行

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 address

initially

SomeClass someClassObj = null; your object is pointing to nothing. when you create a new instance using new () your object starts pointing to newly allocated memory's address. So, inside loop, every instance is assigned new address and that address is stored in SomeClass which is same pointer/reference. When you add item in the list, it actually adds the address/reference of item that is currently pointed by SomeClass

少女情怀诗 2024-12-06 11:51:15

您实际上要做的是在每次迭代时为 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 to someClassObj to your list. The Add method would need to take a ref parameter in order for it to be a reference to someClassObj.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文