Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A constructor in C# is a specialized member function invoked automatically by the Common Language Runtime (CLR) during the instantiation of a class or struct. Its architectural role is to initialize the memory allocated for the object and establish the initial state of its members. Constructors, alongside inline field initializers and init accessors (introduced in C# 9), are the exclusive locations where readonly fields can be assigned or modified. A constructor must share the exact identifier of its enclosing type and explicitly omits a return type, including void.
public class Entity
{
    private readonly string _identifier;

    // Constructor declaration
    public Entity()
    {
        // Initialization logic executes upon instantiation
        _identifier = "INIT_01"; 
    }
}

Constructor Classifications

1. Default (Parameterless) Constructor A constructor that accepts no arguments. If a class contains no explicit constructor declarations, the C# compiler implicitly generates a public parameterless default constructor. For classes, the moment any explicit constructor (parameterized or otherwise) is defined, the compiler ceases to provide the implicit default constructor. For struct types, the compiler always provides an implicit parameterless constructor that zero-initializes fields; however, since C# 10, developers can define an explicit parameterless constructor for a struct to override this default behavior during new invocations.
public class ParameterlessClassExample
{
    public ParameterlessClassExample() { }
}

public struct ParameterlessStructExample
{
    // Explicit parameterless constructor in a struct (C# 10+)
    public ParameterlessStructExample() 
    {
        // Initialization logic
    }
}
2. Parameterized Constructor Constructors can define parameters to accept arguments during instantiation. C# supports constructor overloading, allowing a single type to define multiple constructors provided their method signatures (parameter types, count, or order) differ.
public class ParameterizedExample
{
    public ParameterizedExample(int id, string name) { }
    public ParameterizedExample(string name) { } // Overloaded
}
3. Static Constructor A static constructor initializes type-level (static) data rather than instance data. It is declared using the static modifier, cannot contain access modifiers (e.g., public, private), and cannot accept parameters. The CLR guarantees a static constructor is invoked thread-safely exactly once per type per AssemblyLoadContext or process in modern .NET, executing before the first instance is created or any static members are referenced.
public class StaticExample
{
    static StaticExample()
    {
        // Executes once per AssemblyLoadContext/process
    }
}
4. Private Constructor A constructor decorated with the private access modifier. It restricts instantiation of the type from any context outside the declaring class itself.
public class PrivateExample
{
    private PrivateExample() { }
}

Constructor Chaining

C# allows constructors to invoke other constructors to share initialization logic. This is achieved using the this and base keywords. The chained constructor executes prior to the body of the calling constructor.
  • this(...): Invokes another constructor within the same class.
  • base(...): Invokes a constructor in the direct base class. If base is not explicitly called, the compiler implicitly inserts a call to the base class’s parameterless constructor (base()).
public class BaseEntity
{
    public BaseEntity(int id) { }
}

public class DerivedEntity : BaseEntity
{
    // Chains to the base class constructor
    public DerivedEntity(int id, string name) : base(id)
    {
    }

    // Chains to another constructor in the same class
    public DerivedEntity(int id) : this(id, "Unknown")
    {
    }
}

Primary Constructors

Originally introduced in C# 9 for record types, primary constructors were expanded in C# 12 to support standard class and struct types. They allow parameter declarations directly on the type definition. These parameters are scoped to the entire body of the type and are typically used to initialize properties or fields. If a type defines a primary constructor, any explicitly defined secondary constructors must chain to it using this(...).
// Primary constructor parameters defined at the record level (C# 9+)
public record PersonRecord(string FirstName, string LastName);

// Primary constructor parameters defined at the class level (C# 12+)
public class PrimaryExample(int id, string name)
{
    public int Id { get; } = id;
    public string Name { get; } = name;

    // Secondary constructor chaining to the primary constructor
    public PrimaryExample() : this(0, "Default") { }
}
Master C# with Deep Grasping Methodology!Learn More