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

A parameterized constructor is a special class or struct method invoked during object instantiation that accepts one or more arguments. It allows the caller to inject specific values into the object's state immediately upon creation, directly initializing instance fields or properties.

## Syntax and Mechanics

A parameterized constructor shares the exact name of its enclosing type and has no return type. Its signature is defined by the types, order, and number of parameters it accepts.

```csharp theme={"dark"}
public class NetworkClient
{
    private readonly string _host;
    private readonly int _port;

    // Parameterized Constructor
    public NetworkClient(string host, int port)
    {
        _host = host;
        _port = port;
    }
}
```

## Technical Characteristics

**Compiler Suppression (Classes vs. Structs)**
When you explicitly define a parameterized constructor in a `class`, the C# compiler suppresses the automatic generation of the implicit parameterless (default) constructor. If the class must also support instantiation without arguments, you must explicitly declare a parameterless constructor alongside the parameterized one.

Conversely, for a `struct`, the compiler always retains an implicit parameterless constructor (which zero-initializes the struct's memory) regardless of how many parameterized constructors are explicitly defined.

**Constructor Overloading**
A type can declare multiple parameterized constructors. The C# compiler resolves which constructor to invoke based on the method signature matching the arguments provided during the `new` allocation.

**Member Disambiguation**
When constructor parameter names perfectly match the names of instance fields, the `this` keyword is required within the constructor body to distinguish the instance member from the locally scoped parameter.

```csharp theme={"dark"}
public class User
{
    private string username;

    public User(string username)
    {
        // 'this.username' refers to the instance field
        // 'username' refers to the parameter
        this.username = username; 
    }
}
```

**Constructor Chaining**
Parameterized constructors can route initialization logic to other constructors using constructor initializers.

* `: this(...)` invokes another constructor within the same class.
* `: base(...)` invokes a parameterized constructor in the base class.

```csharp theme={"dark"}
public class TcpClient : NetworkClient
{
    private readonly int _timeout;

    // Chains to the base class parameterized constructor
    public TcpClient(string host, int port, int timeout) : base(host, port)
    {
        _timeout = timeout;
    }

    // Chains to another parameterized constructor in the same class
    public TcpClient(string host) : this(host, 80, 5000)
    {
    }
}
```

## Primary Constructors (C# 12+)

Starting in C# 12, parameterized constructors can be declared directly on the class or struct declaration line. These are known as Primary Constructors. The parameters defined in a primary constructor are in scope throughout the entire body of the type and can be used to initialize properties or fields directly.

```csharp theme={"dark"}
// C# 12 Primary Constructor syntax
public class DatabaseConnection(string connectionString, int timeout)
{
    public string ConnectionString { get; } = connectionString;
    public int Timeout { get; } = timeout;
}
```

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