Jedyna różnica jest w momencie w którym nastąpi przypisanie.
using System;
class A
{
public A()
{
PrintFields();
}
public virtual void PrintFields() { }
}
class B : A
{
int x { get; set; } = 1;
int y { get; set; }
public B()
{
y = -1;
}
public override void PrintFields()
{
Console.WriteLine("x = {0}, y = {1}", x, y);
}
}
public class Test
{
public static void Main()
{
var b = new B();
}
}
Wynik : x = 1, y = 0
Ze specyfikacji języka:
Variable initializers are transformed into assignment statements, and these assignment statements are executed before the invocation of the base class instance constructor. This ordering ensures that all instance fields are initialized by their variable initializers before any statements that have access to that instance are executed.