何时复制 C# 值/对象,何时复制其引用? [英] When is a C# value/object copied and when is its reference copied?

查看:14
本文介绍了何时复制 C# 值/对象,何时复制其引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我想引用的对象被复制或我想复制的对象被引用的地方,我一遍又一遍地遇到同样的问题.当我使用 = 运算符时会发生这种情况.

I keep getting the same issue over and over again where an object I want to reference is copied or where an object I want to copy is referenced. This happens when I use the = operator.

例如,如果我将对象发送到另一个表单,即:

For example, if I am sending the object to another form, ie:

SomeForm myForm = new SomeForm();
SomeObject myObject = new SomeObject();
myForm.formObject = myObject;

...然后修改表单中的对象,原始对象不会被修改.就好像对象被复制而不是被引用.然而,当我这样做时:

...and then modify the object in the form, the original object does not get modified. It is as if the object was copied and not referenced. Yet, when I do this:

SomeObject myObject = new SomeObject();
SomeObject anotherObject = new SomeObject();
anotherObject = myObject;

...然后修改anotherObjectmyObject 也会被修改.

...and then modify anotherObject, myObject gets modified as well.

最严重的情况是当我尝试克隆我定义的对象之一时:

The most aggravating case is when I try to Clone one of my defined objects:

public class SomeObject
{
    double value1, value2;

    //default constructor here

    public SomeObject(val1, val2)
    {
        value1 = val1;
        value2 = val2;
    }

    public void Clone(SomeObject thingToCopy)
    {
        this.value1 = thingToCopy.value1;
        this.value2 = thingToCopy.value2;
    }
}

当我这样做时...

SomeObject obj1 = new SomeObject(1, 2);
SomeObject obj2 = new SomeObject();
obj2.Clone(obj1);

...obj1 被引用,对 obj2 的任何修改都会改变 obj1.

...obj1 is referenced and any modifications to obj2 changes obj1.

int、double、string 等系统对象似乎总是被复制,除了上面的 clone 方法.

System objects such as int, double, string, etc seem to always be copied, except for in the case of the clone method above.

我的问题是,不考虑在函数中使用 ref 关键字,何时复制对象以及何时在每种情况下引用对象(即当传递函数,设置为其他对象时(如前两个示例),复制成员变量时如第三个示例等)?

My question is, not taking into account the use of the ref keyword in functions, when does an object get copied and when does an object get referenced in every case of the matter (i.e. when passing to functions, when setting as other objects (like the first two above examples), when copying member variables like the third example, etc.)?

推荐答案

如果不花大量时间仔细挑选的话,很难准确地回答此类问题.

It's hard to answer this sort of question precisely without spending an awful lot of time picking your words carefully.

我在几篇文章中已经这样做了,您可能会觉得有用:

I've done so in a couple of articles which you may find useful:

当然,这并不是说这些文章是完美的 - 远非如此 - 但我已尽力做到尽可能清楚.

That's not to say that the articles are perfect, of course - far from it - but I've tried to be as clear as I can.

我认为一件重要的事情是在头脑中将两个概念(参数传递和引用与值类型)分开.

I think one important thing is to separate the two concepts (parameter passing and reference vs value types) out in your head.

查看您的具体示例:

SomeForm myForm = new SomeForm();
SomeObject myObject = new SomeObject();
myForm.formObject = myObject;

这意味着 myForm.formObjectmyObject 指的是 SomeObject 的同一个实例——就像两个人有不同的纸,每一个都写着相同的地址.如果你去一张纸上的地址把房子涂成红色,然后去第二张纸上的地址,你会看到一个红色的房子.

This means that myForm.formObject and myObject refer to the same instance of SomeObject - like two people having separate pieces of paper, with each one having the same address written on them. If you go to the address on one piece of paper and paint the house red, then go to the address on the second piece of paper, you'll see a red house.

然后修改表单中的对象"是什么意思不清楚,因为您提供的类型是不可变的.没有办法修改对象本身.您可以更改 myForm.formObject 以引用 SomeObject 的不同实例,但这就像在一张纸上写下地址并在上面写下不同的地址.那不会改变另一张纸上写的内容.

It's not clear what you mean by "and then modify the object in the form" because the type you have provided is immutable. There's no way of modifying the object itself. You can change myForm.formObject to refer to a different instance of SomeObject, but that's like scribbling out the address on one piece of paper and writing a different address on it instead. That won't change what's written on the other piece of paper.

如果你能提供一个简短但完整的程序,它的行为你不理解(最好是一个控制台应用程序,只是为了让事情更短更简单),那么具体地谈论事情会更容易条款.

If you could provide a short but complete program whose behaviour you don't understand (ideally a console application, just to keep things shorter and simpler) it would be easier to talk about things in concrete terms.

这篇关于何时复制 C# 值/对象,何时复制其引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆