Curious Minds

Clear answers to science and history questions

Why C# 12 Primary Constructors Run in Reverse Order

· 6 min read · Science

C# 12 primary constructors execute their parameter assignments in the opposite order from how they appear in the parameter list—the last parameter is assigned first, and the first parameter is assigned last. This reversal is not a bug or an arbitrary quirk; it is a deliberate consequence of how the compiler lowers primary constructor parameters into captured fields and how field initializers interact with the base class constructor call.

Background: What Primary Constructors Actually Do

Primary constructors, introduced in C# 12, let you declare constructor parameters directly on a class or struct declaration. For example:

public class Person(string firstName, string lastName)
{
    public string FirstName { get; } = firstName;
    public string LastName { get; } = lastName;
}

Behind the scenes, the compiler generates a constructor that takes those parameters and assigns them to hidden fields (often called captured fields) so they can be used in field initializers, property initializers, and methods. The order in which those hidden fields are assigned is what appears reversed.

How the Compiler Lowers Primary Constructor Parameters

When you write a primary constructor, the compiler must decide where to store the parameter values. For parameters that are used outside the constructor body (for example, in property initializers or methods), the compiler creates a private field for each parameter. The generated constructor then assigns each parameter to its corresponding field.

The key detail is that the compiler emits these field assignments in reverse order of the parameter list. So for Person(string firstName, string lastName), the generated code assigns lastName to its field first, then firstName. This reversal is consistent across both classes and structs.

Why reverse? The C# language design team chose this order to ensure that field initializers that reference other parameters see a consistent state. In particular, when a field initializer uses a parameter, that parameter's field must already be assigned. By assigning parameters in reverse order, the compiler guarantees that any parameter referenced by a later parameter's initializer is already available. This is similar to how local variable initialization order works in some other languages, but the exact rationale is rooted in the C# specification's rules for primary constructor bodies.

Why the Reversal Matters: Interaction with Field Initializers

Consider a class where one property initializer depends on another parameter:

public class Rectangle(int width, int height)
{
    public int Width { get; } = width;
    public int Height { get; } = height;
    public int Area { get; } = width * height;
}

Here, Area uses both width and height. The compiler must ensure that when Area is initialized, both width and height fields are already set. If assignments happened in forward order, width would be set first, then height, and Area would be computed after both—so forward order would also work in this simple case. The reversal becomes important when initializers themselves have side effects or when base class constructors are involved.

In classes with a base class, the base constructor runs before the derived class's field initializers. Primary constructor parameters are captured in fields that are assigned before the base constructor call? Actually, the C# specification states that primary constructor parameters are in scope throughout the class body, and the compiler generates field assignments that occur before the base constructor call. The reverse order ensures that if a base constructor calls a virtual method that accesses a derived property, the property's backing field might not yet be assigned—but that's a separate concern. The reversal is primarily about making the generated code deterministic and avoiding subtle bugs with parameter dependencies.

A frequent misconception is that primary constructor parameters are evaluated in reverse order. They are not—the arguments you pass at the call site are evaluated left-to-right as usual. Only the internal field assignments are reversed. Another misconception is that this reversal affects the order of side effects in parameter expressions. It does not; side effects in argument expressions happen in the order you write them.

Another related question: does the reversal apply to records? Records with primary constructors (positional records) also generate properties, but the assignment order for positional records is forward, not reversed. The reversal is specific to primary constructors on classes and structs, not to positional records. This difference often surprises developers.

Practical Implications for Developers

In most code, the reversal is invisible because field initializers rarely depend on assignment order. However, it can matter in these situations:

  • Initializers with side effects: If a field initializer calls a method that logs or mutates state, the order of those calls follows the reverse assignment order. For example, if first and second parameters each have initializers that log, the log for second appears before the log for first.
  • Base class constructor calls: If a base constructor accesses a virtual property that is backed by a primary constructor parameter, the property may be uninitialized (null or default) because the derived field assignments happen after the base constructor. This is a known hazard with virtual calls in constructors, not specific to primary constructors.
  • Debugging: When stepping through code, you may see assignments in reverse order, which can be confusing if you expect source order.

To avoid surprises, keep field initializers simple and avoid relying on the order of parameter assignments. If you need a specific order, use a regular constructor with explicit assignments.

Comparison with Regular Constructors and Records

Regular constructors execute statements in the order you write them. If you assign parameters to fields manually, you control the order. Primary constructors hide this control, and the compiler's reverse order is an implementation detail you cannot change. Positional records, by contrast, generate properties in forward order and do not exhibit the reversal. This makes records more predictable for developers who care about initialization order.

The reversal also does not apply to primary constructors on structs? Actually, it does—structs with primary constructors also assign captured fields in reverse order. The behavior is consistent across classes and structs.

Frequently Asked Questions

Why does C# 12 reverse the order of primary constructor parameter assignments?

The C# language design team chose reverse order to ensure that field initializers that reference other parameters see a consistent state, particularly when parameters are used in multiple initializers. The exact rationale is documented in the C# specification and design notes, but the practical effect is deterministic assignment order that avoids certain dependency issues.

Does the reversal affect the order of argument evaluation at the call site?

No. Arguments are evaluated left-to-right as in any method call. Only the internal field assignments generated by the compiler are reversed.

Do positional records also reverse parameter assignment order?

No. Positional records generate properties in forward order. The reversal is specific to primary constructors on classes and structs.

Can I control the assignment order in a primary constructor?

Not directly. If you need a specific order, use a regular constructor with explicit field assignments. Primary constructors are intended for simple initialization scenarios.

Is the reversal a bug that might be fixed?

It is by design and documented in the C# specification. It is unlikely to change because changing it could break existing code that relies on the current behavior, even if inadvertently.

Does the reversal apply to structs as well as classes?

Yes. Primary constructors on structs also assign captured fields in reverse order.

Summary

The reverse execution order of C# 12 primary constructor parameter assignments is a deliberate compiler implementation detail tied to field initialization and dependency management. While it rarely affects everyday code, understanding it helps avoid confusion when debugging or when writing initializers with side effects. For full control, prefer regular constructors.

In this article
  1. Background: What Primary Constructors Actually Do
  2. How the Compiler Lowers Primary Constructor Parameters
  3. Why the Reversal Matters: Interaction with Field Initializers
  4. Common Misconceptions and Related Questions
  5. Practical Implications for Developers
  6. Comparison with Regular Constructors and Records
  7. Frequently Asked Questions
  8. Summary