Codectionary / Developer documentation / C#

Constructors

A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.

Read the syntax

A constructor has the class name and no return type. new selects a constructor whose parameters match the supplied arguments, then that constructor initializes the object.

Overloaded constructors offer different inputs. : this(...) delegates to another constructor of the same class; : base(...) invokes a base constructor. A primary constructor places parameters on the class declaration.

Syntax

public ClassName(parameters) {\n  // initialization code\n}

Examples

Basic Constructor

Initializing fields when an object is created.

Point p = new Point(3, 4);
Console.WriteLine($"({p.X}, {p.Y})");  // (3, 4)

public class Point
{
    public int X, Y;

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}
  1. Initializing fields when an object is created.
  2. A constructor has the class name and no return type. new selects a constructor whose parameters match the supplied arguments, then that constructor initializes the object.
  3. new Point(3, 4) passes values into the constructor, which stores them in X and Y. Interpolation then prints (3, 4).

Constructor Overloading

Providing multiple constructors with different parameter lists for flexibility.

Rectangle r1 = new Rectangle();       // 1x1
Rectangle r2 = new Rectangle(5);       // 5x5 square
Rectangle r3 = new Rectangle(4, 6);    // 4x6

public class Rectangle
{
    public double Width, Height;

    public Rectangle()
    {
        Width = 1;
        Height = 1;
    }

    public Rectangle(double side)  // square shortcut
    {
        Width = side;
        Height = side;
    }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }
}
  1. Providing multiple constructors with different parameter lists for flexibility.
  2. A constructor has the class name and no return type. new selects a constructor whose parameters match the supplied arguments, then that constructor initializes the object.
  3. The three overloads initialize a 1-by-1 rectangle, a 5-by-5 square and a 4-by-6 rectangle. This snippet constructs them but contains no output call.

Constructor Chaining with this()

Having one constructor call another in the same class to avoid duplicating initialization logic.

User u1 = new User("John");           // John (member)
User u2 = new User("Zain", "admin");   // Zain (admin)

public class User
{
    public string Name;
    public string Role;

    public User(string name) : this(name, "member")
    {
    }

    public User(string name, string role)
    {
        Name = name;
        Role = role;
    }
}
  1. Having one constructor call another in the same class to avoid duplicating initialization logic.
  2. A constructor has the class name and no return type. new selects a constructor whose parameters match the supplied arguments, then that constructor initializes the object.
  3. The one-argument constructor forwards to the two-argument constructor with member as the default role. Both User objects receive Name and Role, but this snippet does not print them.

Primary Constructors (C# 12+)

Modern C# lets you declare constructor parameters directly on the class, reducing boilerplate.

Point p = new Point(3, 4);
Console.WriteLine(p.DistanceFromOrigin());  // 5

public class Point(int x, int y)
{
    public int X { get; } = x;
    public int Y { get; } = y;

    public double DistanceFromOrigin() => Math.Sqrt(X * X + Y * Y);
}
  1. Modern C# lets you declare constructor parameters directly on the class, reducing boilerplate.
  2. A constructor has the class name and no return type. new selects a constructor whose parameters match the supplied arguments, then that constructor initializes the object.
  3. The primary constructor parameters initialize read-only X and Y. DistanceFromOrigin computes the square root of 3 squared plus 4 squared and prints 5.

Best practices

  • Use : this(...) to chain constructors and avoid duplicating initialization logic across multiple overloads
  • Validate arguments inside constructors (throwing an exception for invalid values) to prevent objects from ever existing in an invalid state
  • Keep constructors focused on initialization - avoid putting complex business logic or I/O operations inside them
  • Consider primary constructors (C# 12+) for simple classes where the constructor parameters map directly onto properties

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading