Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Constructor Execution Order

Understand the exact order in which C# constructors run, including field initializers, base constructor calls, static constructors, and constructor chaining.

~6 min readC#
#constructor#inheritance#object initialization#runtime behavior
A visual representation of C# constructor execution order showing the sequence of base and derived constructor calls.

When you create an instance of a class in C#, the runtime follows a strict sequence for invoking constructors. The order is not simply "from the most derived class down" or "from the base class up". Instead, it involves a mix of field initializers, base constructor calls, and the constructor body itself. Understanding C# constructor execution order helps you predict when side effects occur, debug initialization errors, and design classes that behave predictably under inheritance.

The core rule is: field initializers in the derived class run before the base constructor is invoked, and the base constructor then runs its own field initializers before its own body. More precisely, when constructing an instance of a derived class:

  1. Instance field initializers in the derived class run.
  2. The base constructor is invoked. It runs its own instance field initializers, then its constructor body.
  3. The derived constructor body runs after the base constructor returns.

For deeper inheritance chains, step 2 is repeated for each base constructor.

Here's a minimal example that demonstrates the order:

csharp
public class Base { public Base() { Console.WriteLine("Base constructor body"); } } public class Derived : Base { private readonly int _value = LogAndReturn("Derived field initializer"); public Derived() { Console.WriteLine("Derived constructor body"); } private static int LogAndReturn(string message) { Console.WriteLine(message); return 1; } } // Creating an instance of Derived prints: // Derived field initializer // Base constructor body // Derived constructor body

The field initializer in Derived runs before the Base constructor is called. This is a common source of confusion because it feels like base initialization should happen first. Field initializers are injected into the constructor's start, and they must be executed before the base constructor call.

The Role of Base Constructor Arguments

When a derived constructor explicitly calls a base constructor using the : base(...) syntax, that call is placed after the derived class's field initializers. The order becomes:

  1. Derived field initializers run.
  2. The base constructor (with its own field initializers and body) executes.
  3. The derived constructor body runs.

Consider this example:

csharp
public class Base { public Base(string message) { Console.WriteLine($"Base: {message}"); } } public class Derived : Base { private readonly int _id = GetId(); public Derived() : base("from derived") { Console.WriteLine($"Derived id: {_id}"); } private static int GetId() { Console.WriteLine("Derived field initializer"); return 42; } }

Output when instantiating Derived:

code
Derived field initializer Base: from derived Derived id: 42

The argument to base(...) is evaluated after the derived field initializers, before the base constructor runs. If you write base(ComputeSomething()), ComputeSomething() runs after the derived field initializers. This timing is rarely important, but it becomes relevant when field initializers depend on external state that the base constructor would normally set up.

Field Initializers and Constructor Bodies: The Injection Point

The C# compiler inserts instance field initializers at the beginning of a constructor before the base constructor call. If a constructor delegates to another constructor on the same class with this(...), the delegating constructor does not repeat them; the constructor that eventually calls base runs them once. If a class has multiple constructors that do not delegate, each of those constructors contains the same field initializer code, but a single constructor call executes it only once.

csharp
public class Example { private readonly List<int> _data = new List<int>(); public Example() { } public Example(int capacity) { _data.Capacity = capacity; } }

In the code above, new List<int>() is created before either constructor body runs. The second constructor then modifies _data.Capacity. If the field initializer throws, the constructor never executes.

A subtle consequence: virtual calls made while a base constructor or a field initializer runs can dispatch to the most derived override before the derived constructor body has executed. If that override reads fields assigned only in the derived constructor body, it sees default values. This is a known pitfall.

Static Constructors and Their Timing

Static constructors are a separate concern from instance constructors. A static constructor runs once per type, before any instance is created or any static member is accessed. The exact point at which it fires is implementation-dependent, so avoid depending on lazy static constructor timing for deterministic behavior. When a type is initialized, its static field initializers run in textual order, then its static constructor body runs.

For derived classes, the base type's static constructor runs before the derived type's static constructor when the derived type is initialized:

csharp
public class Base { static Base() => Console.WriteLine("Base static ctor"); } public class Derived : Base { static Derived() => Console.WriteLine("Derived static ctor"); } // Creating an instance of Derived or accessing a static member // of Derived triggers both, in this order: // Base static ctor runs first, then Derived static ctor.

Do not rely on the exact point when a static constructor fires. If you need deterministic initialization, explicitly call a static method rather than depending on lazy static constructor timing.

Constructor Chaining: Using this(...)

Inside a class, you can chain constructors using this(...). When a constructor uses this(...), it delegates to another constructor on the same class. The target constructor runs the instance field initializers and calls the base constructor. After the target constructor returns, the delegating constructor's body runs.

csharp
public class Point { public int X { get; } public int Y { get; } public Point() : this(0, 0) { Console.WriteLine("Parameterless ctor"); } public Point(int x, int y) { X = x; Y = y; Console.WriteLine("Parameterized ctor"); } }

Instantiating new Point() prints:

code
Parameterized ctor Parameterless ctor

The this chain runs the parameterized constructor first, then the parameterless body. This pattern avoids duplicated initialization logic. Because the constructor that calls the base constructor runs the field initializers, a single chained construction executes them once.

Virtual Calls During Construction

Calling a virtual method from a constructor is dangerous. When the base constructor runs, the derived constructor body has not executed yet, yet virtual dispatch calls the most derived override. If that override depends on fields assigned in the derived constructor body, you see null or default values.

csharp
public class Base { public Base() => Print(); public virtual void Print() => Console.WriteLine("Base"); } public class Derived : Base { private readonly string _name; public Derived() { _name = "Derived"; } public override void Print() => Console.WriteLine(_name); }

Creating new Derived() prints a blank line because _name is still null when Base's constructor calls Print(). Virtual dispatch reaches Derived.Print() before the derived constructor body has run. If _name were set by a field initializer instead, it would print Derived, because field initializers run before the base constructor call. The safe pattern is to avoid virtual calls in constructors altogether.

When the Order Matters in Real Code

Consider a logging or tracing scenario where you want to record the construction sequence. The order described here directly affects the log output, so misinterpreting it leads to confusing diagnostics. Similarly, if a base constructor expects certain services to be set up by the derived class, you'll see flaky behavior. Always initialize dependencies in the base constructor itself or pass them via parameters.

Another practical case is object pooling. A pool might reuse instances by calling an internal Reset method. The constructor order never runs again, but the Reset method must replicate the same initialization steps to avoid stale state.

Edge Cases and Compatibility Notes

  • Structs: A struct cannot declare a parameterless instance constructor before C# 10, and field initializers are not allowed in structs before C# 10 either. Constructor behavior for value types differs in those versions.
  • Records: Records use the same constructor order, but they also include synthesized members like PrintMembers and equality members. The primary constructor follows the same underlying initialization order.
  • Partial classes: Partial class declarations are merged into one type. Field initializer order across partial files is a compiler detail, so avoid depending on cross-file ordering.
  • Async constructors: You cannot have an async constructor. Use a factory method or an InitializeAsync pattern. Constructor order is synchronous, so any async work must happen after construction.

Final Guidance on Predictable Initialization

To keep your code maintainable, follow these rules of thumb:

  • Use field initializers for simple constants or defaults that do not depend on constructor parameters.
  • Prefer passing required dependencies through the constructor parameter list over relying on field initializers that call external services.
  • Avoid virtual calls in any constructor, including base constructors.
  • If you need complex initialization, consider a static factory method that calls a private constructor and then performs additional setup.
  • If you have multiple constructors, have them chain to a shared constructor rather than duplicating initialization logic.

Understanding the exact sequence—field initializers, base constructor, then derived body—lets you predict side effects and avoid subtle bugs. The instance constructor ordering is stable across C# versions; static constructors and records have their own caveats. Keep the mental model simple: field initializers go first, then the chain of base constructors, then the constructor body you wrote.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.