Skip to main content
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.

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.
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.
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.
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.

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()).

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(...).
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More