在C#中引用类型的赋值 [英] Value assignment for reference type in C#

查看:142
本文介绍了在C#中引用类型的赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是值的引用类型来实现分配的正确方法?我想执行一个任务,但不能更改参考。

What is the proper way to implement assignment by value for a reference type? I want to perform an assignment, but not change the reference.

这里是我说的:

void Main()
{
    A a1 = new A(1);
    A a2 = new A(2);
    a1 = a2; //WRONG: Changes reference
    a1.ValueAssign(a2); //This works, but is it the best way?
}

class A
{
    int i;

    public A(int i)
    {
        this.i = i;
    }

    public void ValueAssign(A a)
    {
        this.i = a.i;
    }
}

有约定的某种形式,我应该使用这个?我觉得我不是那种遇到这个的第一人。谢谢你。

Is there some sort of convention I should be using for this? I feel like I'm not the first person that has encountered this. Thanks.

编辑:

哇。我想我需要调整我的问题更倾向于我现在面临的实际问题。我收到了很多不符合不改变基准的要求答案。克隆是不是这里的问题。问题在于分配克隆。

Wow. I think I need to tailor my question more toward the actual problem I'm facing. I'm getting a lot of answers that do not meet the requirement of not changing the reference. Cloning is not the issue here. The problem lies in ASSIGNING the clone.

我有一个依赖于许多类 - 他们都有一个引用类A.那么的相同的对象,每当出现一个班一个变化,它反映了别人,对吧?这一切都很好,很好,直到其中一个类试图做到这一点:

I have many classes that depend on A - they all share a reference to the same object of class A. So, whenever one classes changes A, it's reflected in the others, right? That's all fine and well until one of the classes tries to do this:

myA = new A();

在现实中我没有做新的A(),但实际上,我检索的序列化版本关闭硬盘驱动器。但不管怎么说,这样做会导致妙收到一个新的参考。它不再共享相同的A作为依赖于A.类的其余部分这是我试图解决这个问题。我想受的code以上。

In reality I'm not doing new A() but I'm actually retrieving a serialized version of A off the hard drive. But anyways, doing this causes myA to receive a NEW REFERENCE. It no longer shares the same A as the rest of the classes that depend on A. This is the problem that I am trying to address. I want all classes that have the instance of A to be affected by the line of code above.

我希望这澄清了我的问题。谢谢你。

I hope this clarifies my question. Thank you.

推荐答案

我希望能有一个次优的答案的选择,因为任何人谁提到观察员应得的。观察者模式会工作,但它是没有必要的,在我看来,是矫枉过正。

I wish there was a "second best" answer option, because anyone who mentioned Observer deserves it. The observer pattern would work, however it is not necessary and in my opinion, is overkill.

如果多个对象需要保持引用同一个对象(MyClass的,下同),你需要执行的任务的被引用的对象(MyClass的),最简单的方法处理它是创建ValueAssign功能如下:

If multiple objects need to maintain a reference to the same object ("MyClass", below) and you need to perform an assignment to the referenced object ("MyClass"), the easiest way to handle it is to create a ValueAssign function as follows:

public class MyClass
{
	private int a;
	private int b;

	void ValueAssign(MyClass mo)
	{
		this.a = mo.a;
		this.b = mo.b;
	}
}

观察员只会是必要的,如果的的行动需要在转让时的相关对象。如果你希望只维持了基准,这种方法是足够的。这里这个例子是一样的,我提出我的问题的例子,但我觉得它更强调了我的意图。

Observer would only be necessary if other action was required by the dependent objects at the time of assignment. If you wish to only maintain the reference, this method is adequate. This example here is the same as the example that I proposed in my question, but I feel that it better emphasizes my intent.

感谢你所有的答案。我认真考​​虑他们的一切。

Thank you for all your answers. I seriously considered all of them.

这篇关于在C#中引用类型的赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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