Skip to content

Instantly share code, notes, and snippets.

@ForNeVeR
Last active July 30, 2020 07:31
Show Gist options
  • Save ForNeVeR/7f94bbe02b9b1a6de0031fa39481ccf9 to your computer and use it in GitHub Desktop.
Save ForNeVeR/7f94bbe02b9b1a6de0031fa39481ccf9 to your computer and use it in GitHub Desktop.
C# class constructor/initializer call order
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();
}
}
}
Derived class field init
Base class field init
Base ctor
Derived ctor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment