将变量分配给另一个变量,并将其中一个的更改反映到另一个变量中 [英] Assign Variable to another Variable and have changes in one be mirrored in the other

查看:124
本文介绍了将变量分配给另一个变量,并将其中一个的更改反映到另一个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将一个变量分配给另一个变量,并且当您更改第二个变量时,将瀑布降到第一个变量吗?

Is it possible to assign a variable to another variable and when you change the second variable the change waterfalls down to the first variable?

就像这样。

int a = 0;
int b = a;
b = 1;

现在b和a will均为1。

now both b and a would = 1.

之所以这样问,是因为我要跟踪4个对象,并且使用第5个名为currentObject的对象来跟踪每个对象,该对象等于用户使用的4个对象中的任何一个。但是,我只想对currentObject进行更改,并使其成为来自它的变量的瀑布。

The reason I ask this is because I have 4 objects I'm keeping track of, and I keep track of each using a 5th object called currentObject that equals whichever of the 4 objects the user is using. However, I would like to just make a change to the currentObject and just have it waterfall to the variable it came from.

谢谢

推荐答案

您需要区分对象引用变量。如果您有两个不同的变量(方法中没有通过ref / out进行别名等),则它们将是独立的。

You need to distinguish between objects, references and variables. If you have two different variables (which aren't aliased via ref/out in a method, etc) then those will be independent.

但是,如果两个变量引用相同的 object (即它们的类型是一个类,并且它们的 values 是相等的引用),那么对该对象的任何更改将通过任一变量可见。听起来这就是您想要实现的目标。例如:

However, if two variables refer to the same object (i.e. their type is a class, and their values are equal references), then any changes to that object will be visible via either variable. It sounds like this is what you want to achieve. For example:

public class SomeMutableClass
{
    public string Name { get; set; }
}

// Two independent variables which have the same value
SomeMutableClass x1 = new SomeMutableClass();
SomeMutableClass x2 = x1;

// This doesn't change the value of x1; it changes the
// Name property of the object that x1's value refers to
x1.Name = "Fred";
// The change is visible *via* x2's value.
Console.WriteLine(x2.Name); // Fred

如果您对引用类型和对象的工作方式不完全满意,则不妨阅读我的有关它们的文章

If you're not entirely comfortable with how reference types and objects work, you may wish to read my article about them.

编辑:我经常使用的一个比喻是一所房子。假设我们有两张纸(变量)。两张纸上都写有相同的内部地址(即每个变量的,即 reference )。只有一所房子。如果有人用第一张纸去房子,然后将门漆成红色,那么他们并没有改变纸上的任何东西,而是在改变房子上的东西。然后,如果有人用 second 张纸进入房屋,他们会看到前门也是红色的。只有一所房子,但是很多纸上写着地址。

One analogy I often use is of a house. Suppose we have two pieces of paper (variables). The same house address is written on both pieces of paper (that's the value of each variable, the reference). There's only one house. If someone uses the first piece of paper to get to the house, then paints the door red, they're not changing anything about their piece of paper - they're changing something about the house. Then if someone uses the second piece of paper to get to the house, they'll see the front door is red too. There's only one house, however many pieces of paper have its address written on them.

这篇关于将变量分配给另一个变量,并将其中一个的更改反映到另一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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