相关疑难解决方法(0)

为什么C#不支持引用的返回?

我已经读过.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)

.net c# reference return-type

139
推荐指数
2
解决办法
2万
查看次数

是否可以在C#中返回对变量的引用?

我可以返回对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# reference return-value

29
推荐指数
4
解决办法
3万
查看次数

标签 统计

c# ×2

reference ×2

.net ×1

return-type ×1

return-value ×1