是否存在同义词? [英] Do synonyms exist?

查看:157
本文介绍了是否存在同义词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将一个变量设置为一个对象,并使用另一个始终等于前一个变量的变量?

Is there a way to have a variable set to an object and have another variable which is always equals to the former one?

var x = new object();
var y = [synonym of x];
x = null; // then y = null as well

我不认为这存在.

所以我经常使用数组来保存引用".

So I've often used arrays to hold "references".

var x = new object[] { new object() };
var y = x;
x[0] = null; // then y[0] = null as well

但是感觉有点la脚.

推荐答案

您可以做到,但是您要付出的代价是使用未记录的关键字/功能.他们很久以前就在那里,也许它们不会改变或消失,但是...

You can do it but the price you have to pay is to use undocumented keywords/features. They're there from long time ago and probably they won't change or disappear but...

这将使您的代码阅读起来更加复杂(如果语言本身支持,它可能会很有用),但是它是双向的,您可以在原始对象周围移动,更改也始终会反映在您的引用"中.它与MehmetAtaş答案不同,因为您可以将原始对象传递给另一个功能,并且更改也将传播为您的同义词.它们有局限性(它们不能用于类字段),但它们适用于参数和局部变量.

It'll make your code more complicated to read (it may be useful if supported by the language itself) but it's bidirectional and you can move around your original object, changes will always be reflected to your "reference" too. It differs from Mehmet Ataş answer because you can pass the original object to another function and changes will propagate to your synonym too. They have limitations (they can't be used for class fields) but they works for parameters and local variables.

您需要的是TypedReference,它拥有对另一个变量的引用,然后,如果您为其分配新值,则将更改原始变量.从理论上讲,如果有一天他们会认为包含同义词是一个好功能,那么这可能会打开同义词的大门.

What you need is a TypedReference, it holds a reference to another variable then if you assign a new value to it you'll change the original variable. This in theory could open the door to synonyms if someday they'll think it's a good feature to include.

让我们看一个例子:

var text = "initial value";
var synonym = __makeref(text);

现在,synonym是对文本的引用(请注意,它是对text的引用,而不是对其持有的值的引用).要从TypedReference获取原始值,您可以像这样使用__refvalue:

Now synonym is a reference to text (please note it's a reference to text and not to the value it holds). To get original value from a TypedReference you use __refvalue like this:

Console.WriteLine(__refvalue(synonym, string));

它们具有相同的值:

Debug.Assert(__refvalue(synonym, string) == text);

现在让我们将文本更改为新值:

Now let's change text to a new value:

text = "second value";
Debug.Assert(__refvalue(synonym, string) == text);

甚至相反:

__refvalue(synonym, string) = "third value"; // <---
Debug.Assert(__refvalue(synonym, string) == text);

最后,让我们在另一个函数中修改原始变量(不知道引用,它将看到一个 normal 变量):

Finally let's modify the original variable within another function (unaware of the reference it'll see a normal variable):

void ChangeIt(ref string value) { value = "another value"; }

ChangeIt(ref text);
Debug.Assert(__refvalue(synonym, string) == text);

所有这些作品也将评估类型.请注意,这将创建变量的别名,而不是别名(您可以将它们想象为安全的指针,如果是引用类型,则指向指针).让我们尝试一下:

All of this works will value types as well. Note that this creates a synonym for a variable, not an alias (you can imagine them as a safe pointer - to pointer in case of reference type). Let's try this:

void foo1()
{
    string text = "ABC";
    foo2(text);

    // text holds the original value "ABC"
    // not the value modified in foo2
}

void foo2(string value)
{
   value = "123";
   var synonym = __makeref(value);
   __refvalue(value, string) = "456";

   // both value and synonym holds "456";
}

这篇关于是否存在同义词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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