Lesson 10 — Constructors in C# (Default, Parameterized & Static Constructors Explained)

A constructor is a special method inside a class that runs automatically when an object is created.
Constructors are used to initialize object data, set default values, and ensure the object starts in a valid state.

In this lesson, you’ll learn:

  • What is a constructor?
  • Types of constructors in C#
  • Constructor overloading
  • When and why to use each constructor
  • Practical examples with clear explanations

🌟 What is a Constructor?

A constructor is a special method that:

✔ Has the same name as the class
✔ Has no return type (not even void)
✔ Runs automatically when an object is created

✔ Syntax:

public class ClassName
{
    public ClassName()
    {
        // initialization code
    }
}


🧩 Why Constructors Are Important?

✔ Initialize values

✔ Set default data

✔ Avoid invalid object states

✔ Improve readability

✔ Prepare objects for use

Without constructors, you would need to manually set all values after object creation.


🧱 Types of Constructors in C#

C# supports 3 main types of constructors:

1️⃣ Default Constructor
2️⃣ Parameterized Constructor
3️⃣ Static Constructor

Plus, constructor overloading (multiple constructors).

Let’s learn each.


🟥 1️⃣ Default Constructor (No Parameters)

A default constructor does not take any parameters.
If you do not write one, C# automatically creates it.

✔ Example:

public class Student
{
    public string Name;
    public int Age;

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

Using the class:

Student s = new Student();
Console.WriteLine(s.Name);   // Unknown
Console.WriteLine(s.Age);    // 0

✔ Sets default values
✔ Automatically runs when object is created


🟩 2️⃣ Parameterized Constructor (Takes Parameters)

Used when you want to initialize values while creating the object.

✔ Example:

public class Student
{
    public string Name;
    public int Age;

    public Student(string name, int age)      // Parameterized constructor
    {
        Name = name;
        Age = age;
    }
}

Using the class:

Student s = new Student("Riya", 20);
Console.WriteLine(s.Name);   // Riya
Console.WriteLine(s.Age);    // 20

✔ Allows flexible initialization
✔ Values set at object creation


🔄 Constructor Overloading

You can create multiple constructors in the same class with different parameters.

✔ Example:

public class Car
{
    public string Brand;
    public string Color;

    public Car()                       // Default
    {
        Brand = "Unknown";
        Color = "Black";
    }

    public Car(string brand)           // One parameter
    {
        Brand = brand;
        Color = "Black";
    }

    public Car(string brand, string color)     // Two parameters
    {
        Brand = brand;
        Color = color;
    }
}

Usage:

Car c1 = new Car();
Car c2 = new Car("BMW");
Car c3 = new Car("Audi", "Red");

✔ Same method name
✔ Different parameter list
✔ Compiler identifies which one to run


🔵 3️⃣ Static Constructor (Runs Once Only)

A static constructor:

  • Runs only once
  • Runs automatically before the first object is created
  • Initializes static fields in the class
  • Cannot take parameters
  • Cannot have access modifiers (always private)

✔ Example:

public class Config
{
    public static string COMPANY_NAME;

    static Config()         // Static constructor
    {
        COMPANY_NAME = "TechCorp";
        Console.WriteLine("Static constructor executed!");
    }
}

Usage:

Console.WriteLine(Config.COMPANY_NAME);

Output:

Static constructor executed!
TechCorp

✔ Runs only once
✔ Used to initialize static data


🧠 Default vs Parameterized vs Static Constructor (Comparison)

FeatureDefault ConstructorParameterized ConstructorStatic Constructor
ParametersNoYesNo
Called When?Every time object is createdEvery time object is createdOnce per class
PurposeSet default valuesSet custom valuesInitialize static data
Access ModifierAnyAnyAlways private
OverloadingYesYesNo

🌈 Real-World Analogy of Constructors

✔ Buying a Mobile Phone

When you buy a new phone:

  • Some phones come with default settings → Default constructor
  • You choose a phone with custom RAM/storage → Parameterized constructor
  • The company settings (brand, warranty info) are loaded once → Static constructor

🛠 Practical Example — Bank Account with Constructors

public class BankAccount
{
    public string AccountNumber;
    public double Balance;

    // Default constructor
    public BankAccount()
    {
        AccountNumber = "Not Assigned";
        Balance = 0;
    }

    // Parameterized constructor
    public BankAccount(string acc, double bal)
    {
        AccountNumber = acc;
        Balance = bal;
    }

    // Static constructor
    static BankAccount()
    {
        Console.WriteLine("BankAccount class loaded!");
    }
}

Usage:

BankAccount a1 = new BankAccount();
BankAccount a2 = new BankAccount("12345", 5000);


📝 Mini Exercise

Create a Product class with:

🔹 Fields: Name, Price
🔹 Constructors:

  • Default → sets Name = “Unknown”, Price = 0
  • Parameterized → sets Name & Price
  • Overloaded version → only sets Name
  • Static constructor → prints “Product class initialized”

Test all constructors.


🔍 FAQs (SEO Boost)

Q1: Can a constructor return a value?

No. Constructors do not have return types.

Q2: Can we call a constructor manually?

No. It runs automatically during object creation.

Q3: Can a static constructor take parameters?

No. Static constructors cannot have parameters.

Q4: Can we declare a constructor as private?

Yes, private constructors are used in Singleton patterns.


🎉 Conclusion

You now fully understand:

✔ What constructors are
✔ Why they are important
✔ Default constructors
✔ Parameterized constructors
✔ Static constructors
✔ Constructor overloading
✔ Real-world and C# examples

Constructors are essential for writing clean, safe, and readable C# Object-Oriented code.