我有以下代码:
static void Main(string[] args)
{
var x = 9;
var y = 23;
Console.WriteLine($"x = {x}, y = {y}");
var z = SomeMethod(ref x, y);
Console.WriteLine($"x = {x}, y = {y}, z = {z}");
z = 20;
Console.WriteLine($"x = {x}, y = {y}, z = {z}");
Console.ReadLine();
}
private static ref int SomeMethod(ref int x, int y)
{
x++;
y++;
return ref x;
}
请注意 SomeMethod 方法返回签名上的 ref 关键字。
这会产生以下输出:
x = 9, y = 23
x = 10, y = 23, z = 10
x = 10, y = 23, z = 20
我本想在最后一行得到与z值相同的x值。我错过了什么吗?