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

查看:298
本文介绍了当一个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;

...然后修改 anotherObject myObject的被修改为好。

最恼人的情况是,当我试图克隆我定义的对象之一:

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

系统为 INT,双,字符串等似乎永远被复制,除了上面的克隆方法的情况下对象,如

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.)?

推荐答案

这是很难回答这类问题$ P $的pcisely无需花费非常多的时间仔细挑选你的话。

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:

  • Parameter passing in C# / .NET
  • Reference types and value types in C# / .NET

这并不是说,这些条款是完美的,当然 - 远非如此 - 但我试过要清楚,我可以

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.

我觉得一个重要的事情是两个概念(参数传递和参考VS值类型)在你的头上了分离。

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

要看看你的具体的例子:

To look at your specific examples:

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

这意味着 myForm.formObject myObject的参考 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天全站免登陆