这是复制引用还是对象?

发布于 2024-09-05 13:36:20 字数 1049 浏览 7 评论 0原文

抱歉,我既厚又懒,但主要是懒。事实上,甚至不是这样。我正在努力节省时间,这样我就可以在更少的时间内做更多的事情,因为还有很多事情要做。

这是复制引用还是实际对象数据?

public class Foo
{
    private NameValueCollection _nvc = null;

    public Foo( NameValueCollection nvc)
    {
        _nvc = nvc;
    }
}

public class Bar
{
    public static void Main()
    {
        NameValueCollection toPass = new NameValueCollection();
        new Foo( toPass ); // I believe this only copies the reference
                           // so if I ever wanted to compare toPass and 
                           // Foo._nvc (assuming I got hold of the private 
                           // field using reflection), I would only have to
                           // compare the references and wouldn't have to compare
                           // each string (deep copy compare), right?
}

我想我肯定知道答案:它只复制引用。但我什至不知道为什么我要问这个。

我想我唯一关心的是,在通过使用 toPass 调用其参数化 ctor 实例化 Foo 后,是否需要确保我作为 toPass 传递的 NVC 和 NVC 私有字段 _nvc 具有完全相同的内容,我只需要比较它们的引用,对吧?

Sorry, I am being both thick and lazy, but mostly lazy. Actually, not even that. I am trying to save time so I can do more in less time as there's a lot to be done.

Does this copy the reference or the actual object data?

public class Foo
{
    private NameValueCollection _nvc = null;

    public Foo( NameValueCollection nvc)
    {
        _nvc = nvc;
    }
}

public class Bar
{
    public static void Main()
    {
        NameValueCollection toPass = new NameValueCollection();
        new Foo( toPass ); // I believe this only copies the reference
                           // so if I ever wanted to compare toPass and 
                           // Foo._nvc (assuming I got hold of the private 
                           // field using reflection), I would only have to
                           // compare the references and wouldn't have to compare
                           // each string (deep copy compare), right?
}

I think I know the answer for sure: it only copies the reference. But I am not even sure why I am asking this.

I guess my only concern is, if, after instantiating Foo by calling its parameterized ctor with toPass, if I needed to make sure that the NVC I passed as toPass and the NVC private field _nvc had the exact same content, I would just need to compare their references, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

幽蝶幻影 2024-09-12 13:36:20

是的,这是正确的。但是,如果您稍后想要比较 toPassFoo._nvc,您可能仍然需要进行成员方面的比较,以便不同但等效的集合比较相等。

Yes, that's correct. However, if you want to compare toPass and Foo._nvc later, you still may want to do a member-wise comparison so different but equivalent collections compare equal.

梦纸 2024-09-12 13:36:20

总之,是的。

In a word, yes.

滥情稳全场 2024-09-12 13:36:20

您正在按值传递引用类型。您可以更改引用指向的数据(例如,向集合中添加新项目),并且更改将反映给调用者。正如马修指出的,您可能仍然需要检查不同但等效的对象。

假设您坚持打印的用法并且不执行任何“新”操作,则对象引用将保持等效。

public class Foo 
    { 
        public NameValueCollection _nvc = null;

        public Foo( NameValueCollection nvc) 
        {
            //this is cool
            _nvc = nvc;
            _nvc.Add("updated", "content");

            //this isn't cool. Now you have a new object with equivalent members and you should follow Matthew's advice
            //_nvc = new NameValueCollection();
            //_nvc.Add("foo", "bar");
            //_nvc.Add("updated", "content");
        } 
    }

    public class Bar
    {
        public static void Main()
        {
            NameValueCollection toPass = new NameValueCollection();
            toPass.Add("foo", "bar");
            Foo f = new Foo(toPass); 

            if (Object.Equals(toPass, f._nvc))
                Console.WriteLine("true");
            else
                Console.WriteLine("false");
            Console.ReadLine();
        }
    }

You are passing a reference type by value. You can change the data pointed to by the reference (e.g. add new items to the collection) and the changes will be reflected to the caller. As Matthew pointed out, you may still need to check for different yet-equivalent objects.

Assuming you stick to the usage you printed and do not perform any "new" operations the object references will remain equivalent.

public class Foo 
    { 
        public NameValueCollection _nvc = null;

        public Foo( NameValueCollection nvc) 
        {
            //this is cool
            _nvc = nvc;
            _nvc.Add("updated", "content");

            //this isn't cool. Now you have a new object with equivalent members and you should follow Matthew's advice
            //_nvc = new NameValueCollection();
            //_nvc.Add("foo", "bar");
            //_nvc.Add("updated", "content");
        } 
    }

    public class Bar
    {
        public static void Main()
        {
            NameValueCollection toPass = new NameValueCollection();
            toPass.Add("foo", "bar");
            Foo f = new Foo(toPass); 

            if (Object.Equals(toPass, f._nvc))
                Console.WriteLine("true");
            else
                Console.WriteLine("false");
            Console.ReadLine();
        }
    }
悲喜皆因你 2024-09-12 13:36:20

编辑:是的,这复制了引用的值。

我也同意 Matthew Flaschen 关于深度复制比较的观点 - 并且还重载了您的相等比较运算符

Edit: Yes, this copied the value of the reference.

I also agree with Matthew Flaschen on deep copy compare - and also overload your equality compare operator

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