Derived class field init
Base class field init
Base ctor
Derived ctor
Last active
July 30, 2020 07:31
C# class constructor/initializer call order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp5 | |
{ | |
class Base | |
{ | |
private int _baseField = PrintAndReturnLength("Base class field init"); | |
protected Base() | |
{ | |
Console.WriteLine("Base ctor"); | |
} | |
protected static int PrintAndReturnLength(string message) | |
{ | |
Console.WriteLine(message); | |
return message.Length; | |
} | |
} | |
internal class Derived : Base | |
{ | |
private int _derivedField = PrintAndReturnLength("Derived class field init"); | |
public Derived() | |
{ | |
Console.WriteLine("Derived ctor"); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var d = new Derived(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment