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

# C# Constructor

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

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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()`).

```csharp theme={"dark"}
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(...)`.

```csharp theme={"dark"}
// 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") { }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
