Derived class field init
Base class field init
Base ctor
Derived ctor
Last active
July 30, 2020 07:31
-
-
Save ForNeVeR/7f94bbe02b9b1a6de0031fa39481ccf9 to your computer and use it in GitHub Desktop.
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