</>
Skip to content
C# lessons (20/34)

C# — Constructors

Default constructor

using System;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    // Default constructor
    public Person()
    {
        Name = "Unknown";
        Age = 0;
    }
}

class Program
{
    static void Main()
    {
        var p = new Person();
        Console.WriteLine($"{p.Name}, {p.Age}"); // Unknown, 0
    }
}

Parameterized constructor

using System;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

class Program
{
    static void Main()
    {
        var p = new Person("Alice", 30);
        Console.WriteLine($"{p.Name}, {p.Age}");
    }
}

Constructor chaining

using System;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; }

    // Main constructor
    public Person(string name, int age, string city)
    {
        Name = name;
        Age = age;
        City = city;
    }

    // Delegates to main
    public Person(string name, int age) : this(name, age, "Unknown") { }

    // Delegates with defaults
    public Person(string name) : this(name, 0) { }

    public override string ToString() =>
        $"{Name}, {Age}, {City}";
}

class Program
{
    static void Main()
    {
        var p1 = new Person("Alice", 30, "NYC");
        var p2 = new Person("Bob", 25);
        var p3 = new Person("Charlie");

        Console.WriteLine(p1); // Alice, 30, NYC
        Console.WriteLine(p2); // Bob, 25, Unknown
        Console.WriteLine(p3); // Charlie, 0, Unknown
    }
}

Copy constructor

using System;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    // Copy constructor
    public Person(Person other)
    {
        Name = other.Name;
        Age = other.Age;
    }
}

class Program
{
    static void Main()
    {
        var original = new Person("Alice", 30);
        var copy = new Person(original);

        copy.Name = "Bob";
        Console.WriteLine($"Original: {original.Name}"); // Alice
        Console.WriteLine($"Copy: {copy.Name}");         // Bob
    }
}

Static constructor

using System;

public class Config
{
    public static readonly string AppName;
    public static readonly DateTime StartupTime;

    static Config()
    {
        AppName = "MyApp";
        StartupTime = DateTime.Now;
        Console.WriteLine("Config initialized");
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Config.AppName);      // MyApp
        Console.WriteLine(Config.StartupTime);  // Current time
    }
}

Primary constructor (C# 12)

using System;

// Parameters are directly available as fields
public class Person(string name, int age)
{
    public string Name => name;
    public int Age => age;

    public void Print() => Console.WriteLine($"{name} is {age}");
}

// With inheritance
public class Employee(string name, int age, string department)
    : Person(name, age)
{
    public string Department => department;

    public override string ToString() =>
        $"{Name}, {Age}, {Department}";
}

class Program
{
    static void Main()
    {
        var p = new Person("Alice", 30);
        p.Print(); // Alice is 30

        var e = new Employee("Bob", 25, "Engineering");
        Console.WriteLine(e); // Bob, 25, Engineering
    }
}

Object initializer

using System;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; } = "Unknown";
}

class Program
{
    static void Main()
    {
        // Object initializer syntax
        var p = new Person
        {
            Name = "Alice",
            Age = 30,
            City = "NYC"
        };

        Console.WriteLine($"{p.Name}, {p.Age}, {p.City}");
    }
}

Required members (C# 11)

using System;

public class Person
{
    public required string Name { get; init; }
    public required int Age { get; init; }
    public string? Email { get; init; }
}

class Program
{
    static void Main()
    {
        var p = new Person
        {
            Name = "Alice",  // Required
            Age = 30,        // Required
            Email = "a@b.com" // Optional
        };

        Console.WriteLine($"{p.Name}, {p.Age}");
        // var bad = new Person { Name = "Bob" }; // Error: Age is required
    }
}

Mini Practice

Write C# code that:

  1. Creates a class with constructor chaining
  2. Implements a copy constructor
  3. Uses a primary constructor
  4. Demonstrates required members

Up Next

In the next lesson, you'll learn about Properties — getters, setters, and property patterns.

Related Topics

Frequently Asked Questions about Constructors

What is Constructors in C#?

Constructors is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Constructors?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Constructors.

Why is Constructors important in C#?

Constructors is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.