> ## 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# Primary Constructor

A primary constructor in C# is a concise syntax for declaring constructor parameters directly on a `class` or `struct` declaration. Introduced in C# 12, it scopes the declared parameters to the entire body of the type, allowing them to be used for field initialization, property assignment, or base class invocation without requiring a separate, explicitly defined constructor body.

```csharp theme={"dark"}
public class TypeName(Type param1, Type param2)
{
    // param1 and param2 are available throughout the class body
}
```

## Parameter Semantics and Scope

Unlike primary constructors on `record` types, parameters declared on a standard `class` or `struct` **do not** automatically generate public properties. Instead, they behave as parameters that are in scope throughout the entire instance declaration.

The compiler determines how to allocate memory for these parameters based on their usage:

1. **Initialization Only:** If a parameter is used exclusively to initialize a field or property, it is consumed during object construction, and the compiler does not generate a backing field for the parameter itself.
2. **Member Capture:** If a parameter is referenced within a method, an accessor, or a lambda expression, the compiler automatically generates a private, mutable backing field to capture and store the parameter's state for the lifetime of the object.

```csharp theme={"dark"}
public class StateManager(int initialCapacity, string stateName)
{
    // Consumed during initialization; no backing field generated for 'initialCapacity'
    public int Capacity { get; set; } = initialCapacity;

    // Captured by a method; compiler generates a hidden backing field for 'stateName'
    public void PrintState() => Console.WriteLine(stateName);
    
    // Primary constructor parameters are mutable within the class body
    public void MutateState(string newName) => stateName = newName; 
}
```

## Base Class Invocation

When a derived class utilizes a primary constructor, any required base class constructor arguments are passed directly in the inheritance declaration.

```csharp theme={"dark"}
public class BaseEntity(Guid id)
{
    public Guid Id { get; } = id;
}

// 'id' is passed to the base constructor
public class UserEntity(Guid id, string username) : BaseEntity(id)
{
    public string Username { get; } = username;
}
```

## Constructor Chaining Rules

When a type defines a primary constructor, it becomes the designated master constructor for that type. Any explicitly defined secondary constructors **must** invoke the primary constructor using the `this(...)` initializer. This ensures that the primary constructor parameters are always guaranteed to be initialized.

```csharp theme={"dark"}
public class Configuration(string environment, int timeout)
{
    // Secondary constructor chaining to the primary constructor
    public Configuration(string environment) : this(environment, 30)
    {
        // Additional secondary constructor logic
    }

    // Parameterless constructor chaining to the primary constructor
    public Configuration() : this("Development", 30)
    {
    }
}
```

## Struct-Specific Behavior

When applying a primary constructor to a `struct`, the compiler enforces standard struct initialization rules. The implicit parameterless constructor remains available and will initialize the struct to its default state (zeroing out memory), bypassing the primary constructor entirely unless explicitly invoked.

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