ref 用于变量而不是函数中的参数

发布于 2024-10-15 23:45:45 字数 274 浏览 6 评论 0原文

假设我有一个 Person 类并具有以下内容:

Person A = new Person("Tom");
Person B = A;

有没有办法可以更改它,以便如果我将新的 Person 分配给 B , B = new Person("Harry"), A 也会引用同一个实例吗?我知道您可以在函数中的 ref 参数赋值中做到这一点。

Suppose I have a Person class and have the following:

Person A = new Person("Tom");
Person B = A;

Is there a way I can change it so that if I assign a new Person to B, B = new Person("Harry"), A would be referring to the same instance too? I know you can do that in a ref parameter assignment in a function.

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

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

发布评论

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

评论(3

撑一把青伞 2024-10-22 23:45:45

更新:此处描述的功能已添加到 C# 7 中。


您想要的功能称为“ref locals”,C# 中不支持它。

CLR 确实支持生成包含引用局部变量的代码,几年前我编写了一个 C# 实验版本,它具有您想要的功能,只是为了看看它是否有效。你可以这样做:

Person a = whatever;
ref Person b = ref a;

然后正如你所说,对“b”的更改将更改“a”的内容。这两个变量成为同一存储位置的别名。

尽管这是一个不错的小功能并且运行良好,但我们决定不将其用于 C#。在该语言的假设的未来版本中,这种情况仍有可能发生,但我不会对此感到兴奋;它可能不会发生。

(请记住,Eric 对任何 Microsoft 产品的假设未来版本的所有思考都仅供娱乐。)

UPDATE: The feature described here was added to C# 7.


The feature you want is called "ref locals" and it is not supported in C#.

The CLR does support generating code that contains ref locals, and a few years ago I wrote an experimental version of C# that had the feature you want, just to see if it would work. You could do something like:

Person a = whatever;
ref Person b = ref a;

and then as you say, changes to "b" would change the contents of "a". The two variables become aliases for the same storage location.

Though it was a nice little feature and worked well, we decided to not take it for C#. It's possible that it could still happen in a hypothetical future version of the language, but I would not get all excited about it in expectation; it will probably not happen.

(Remember, all of Eric's musings about hypothetical future versions of any Microsoft product are For Entertainment Purposes Only.)

独行侠 2024-10-22 23:45:45

不,这在安全代码中是不可能的(除了 ref 函数参数)。在不安全的代码中,您可能可以使用指针来实现这一点,但我认为这不是一个好主意。

A 包含对“Tom”的引用作为值。您需要引用 A 来更改它指向的位置。

如果我们考虑一种显式使用指针而不是隐式地将类实例视为引用的语言中的类似代码:

Person* A=new Person("Tom");
Person* B=A;
B=new Person("Harry");//Changes only B
Person** C=&A;
(*C)=new Person("John");//Now A changes

因此您需要一个指向指针的指针才能更改 A。如果将其转移到 C#,则需要对局部变量的引用。这些仅以 ref 函数参数的形式提供。这避免了一些生命周期问题,因为对局部变量的引用不能安全地超过该局部变量的寿命。

如果这些不是私有变量,您可以将 B 设置为在 setter 中修改 A 的属性。

Person B{get{return A;} set{A=value;}}

No it isn't possible in safe code(beyond ref function parameters). In unsafe code you might be able to use pointers to achieve that, but I don't think that's a good idea.

A contains the reference to "Tom" as a value. You'd need a reference to A to change where it points.

If we consider similar code in a language which explicitly uses pointers instead of implicitly treating instances of classes as references:

Person* A=new Person("Tom");
Person* B=A;
B=new Person("Harry");//Changes only B
Person** C=&A;
(*C)=new Person("John");//Now A changes

So you need a pointer to a pointer in order to change A. If you transfer this to C# you'd need a reference to a local variable. And those are only available in the form of ref function parameters. This avoids some lifetime issues since a reference to a local variable can't safely outlive that local variable.

If these are not private variables you could make B a property that modifies A in the setter.

Person B{get{return A;} set{A=value;}}
完美的未来在梦里 2024-10-22 23:45:45

没有直接的方法可以做到这一点。

您可以采用源代码方法,例如:

A = B = new Person("Harry")

或使用泛型类型。类似于:

var A = new ReferenceHolder<Person> (new Person("Tom")); 
var B = A;

在 C# 中,您还可以使用指针(因此基本上是引用类型),但该方法不可静态验证,因此不建议使用。

There is no direct way to do that.

You can either take a source-code approach like:

A = B = new Person("Harry")

Or use a generic type. Something like:

var A = new ReferenceHolder<Person> (new Person("Tom")); 
var B = A;

In C# you could also use a pointer (so basically a ref type) but that approach is not statically verifyable and not suggested.

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