【发布时间】:2021-10-17 17:58:09
【问题描述】:
我打算在数组中返回一个值的引用(并对其进行操作),但是,当我在 method() 中传递数组时,结果不是我想要的。
而且我也很好奇我不能使用DEBUG监视器查询&a在method()堆栈中的地址。
任何人都可以帮助我正确使用return ref吗?
static void Main(string[] args)
{
int?[] s = new int?[5] { 1, 2, 3, 4, 5 };
var q = method(ref s);
q = 999;
Console.WriteLine("s0 is " + s[0].ToString());
}
static ref int? method(ref int?[] a)
{
a[1] = 888;
return ref a[0];
}
【问题讨论】:
-
ref var q = ref method(ref s);• 请参阅Ref locals 和Ref Returns, Ref Locals, and how to use them
标签: c# function reference method-call return-by-reference