C# 7: Ref Local with Instance Field

CSharp · Console

/Program.cs

using System;

public static class Program
{
	public static void Main()
	{
		var obj = new Foo();
		ref int alias = ref obj.Number;
		
		alias = 10;
		Console.WriteLine(obj.Number); //  輸出 "10"
		
		obj = new Foo();
		Console.WriteLine(alias); // 輸出 "0" 還是 "10"?
	}
}

public class Foo
{
	public int Number = 0;
}
;