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.

Syntax

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

Examples

Basic Constructor

Initializing fields when an object is created.

public class Point
{
    public int X, Y;

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

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

Constructor Overloading

Providing multiple constructors with different parameter lists for flexibility.

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

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

Constructor Chaining with this()

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

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

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

Primary Constructors (C# 12+)

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

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);
}

Point p = new Point(3, 4);
Console.WriteLine(p.DistanceFromOrigin());  // 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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Classes & Objects
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.
Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.
Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.
Inheritance & base
Inheritance lets a class (the derived or child class) reuse and extend the members of another class (the base or parent class), written as class Child : Parent. The base keyword gives access to the parent class's members from within the child, most commonly used to call the parent's constructor so the child doesn't have to duplicate its setup logic. Unlike some languages, C# only supports single class inheritance - a class can extend just one base class, but can implement multiple interfaces.