裁判在函数的变量参数不 [英] ref for variables not parameters in functions

查看:166
本文介绍了裁判在函数的变量参数不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类,并有以下几点:

Suppose I have a Person class and have the following:

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



有没有办法我可以改变它,这样如果我给你一个新的 B B =新的Person(哈利) A 将指的是同一个实例吗?我知道你能做到这一点在 REF在函数参数赋值。

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.

推荐答案

你想要的功能被称为REF当地人,它是不是在C#的支持。

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

在CLR确实支持生成包括裁判本地人的代码,和几个几年前,我写了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;



,然后像你说的,更改为B将改变一的内容。这两个变量成为相同的存储位置的别名。

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

虽然是一个不错的功能和运作良好,我们决定不采取它为C#。这是可能的,它仍然可能发生在语言的假想未来版本,但我也不会得到所有兴奋的期待;它可能不会发生。

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.

(请记住,所有Eric的有关未来的任何微软产品的假想版本思索的是仅供娱乐。)

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

这篇关于裁判在函数的变量参数不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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