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 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.
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.
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.
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.
// C# 12 Primary Constructor syntax
public class DatabaseConnection(string connectionString, int timeout)
{
    public string ConnectionString { get; } = connectionString;
    public int Timeout { get; } = timeout;
}
Master C# with Deep Grasping Methodology!Learn More