我已经读过.NET支持返回引用,但C#没有.有特殊原因吗?为什么我不能做这样的事情:
static ref int Max(ref int x, ref int y)
{
if (x > y)
return ref x;
else
return ref y;
}
Run Code Online (Sandbox Code Playgroud) 我可以返回对double值的引用吗?
这就是我想要做的:
ref double GetElement()
{
......
// Calculate x,y,z
return ref doubleArray[x,y,z];
}
Run Code Online (Sandbox Code Playgroud)
要像这样使用它
void func()
{
GetElement()=5.0;
}
Run Code Online (Sandbox Code Playgroud)
这就像在C++中返回一个双指针......我知道我写它的方式是错误的......但是有没有正确的方法呢?
我试图了解如何通过"引用"分配给c#中的类字段.
我有以下示例要考虑:
public class X
{
public X()
{
string example = "X";
new Y( ref example );
new Z( ref example );
System.Diagnostics.Debug.WriteLine( example );
}
}
public class Y
{
public Y( ref string example )
{
example += " (Updated By Y)";
}
}
public class Z
{
private string _Example;
public Z( ref string example )
{
this._Example = example;
this._Example += " (Updated By Z)";
}
}
var x = new X();
Run Code Online (Sandbox Code Playgroud)
运行上面的代码时,输出是:
X(由Y更新)
并不是: …
例如:
int x = 1;
int y = x;
y = 3;
Debug.WriteLine(x.ToString());
Run Code Online (Sandbox Code Playgroud)
是否为任何引用运算符而不是"="在行:3,使x等于3,如果我指定y = 3.
在下面的代码是什么的意义ref的GetAge()方法签名?
public class Person
{
private int age;
public ref int GetAge()
{
return ref this.age;
}
}
Run Code Online (Sandbox Code Playgroud)