C# Constructors Explained: Types, Examples and Usage Guide

C# Constructors Explained: Types, Examples and Usage Guide

A constructor in C# is a special method inside a class that is automatically called when an object is created. It is mainly used to initialize objects and assign default values to fields or properties.

Constructors have the same name as the class and do not return any value, not even void.

1. What is a Constructor in C#?

A constructor is a method that:

• Runs automatically when an object is created
• Initializes object data
• Has the same name as the class
• Has no return type

2. Default Constructor

A default constructor does not take any parameters.

Example:

using System;

class Car
{
    public Car()
    {
        Console.WriteLine("Constructor called!");
    }
}

class Program
{
    static void Main()
    {
        Car myCar = new Car();
    }
}

Output:

Constructor called!

3. Parameterized Constructor

A parameterized constructor accepts values when creating an object.

Example:

using System;

class Car
{
    public string brand;

    public Car(string carBrand)
    {
        brand = carBrand;
    }

    public void Show()
    {
        Console.WriteLine(brand);
    }
}

class Program
{
    static void Main()
    {
        Car car1 = new Car("BMW");
        Car car2 = new Car("Toyota");

        car1.Show();
        car2.Show();
    }
}

4. Constructor Overloading

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

Example:

class Person
{
    public string name;
    public int age;

    public Person()
    {
        name = "Unknown";
        age = 0;
    }

    public Person(string n, int a)
    {
        name = n;
        age = a;
    }
}

5. Types of Constructors

Constructor Type Description
Default Constructor No parameters, initializes default values
Parameterized Constructor Takes parameters to initialize object
Copy Constructor Creates a new object from an existing object
Static Constructor Runs once automatically for class-level initialization

6. Real-World Example

Think of a constructor like buying a car from a factory:

• You choose options (color, engine type)
• The factory builds it automatically
• You get a ready-to-use car

7. Why Use Constructors?

Advantages:

• Automatic initialization
• Cleaner and more organized code
• Prevents uninitialized objects
• Reduces manual setup errors

8. Common Mistakes

• Forgetting constructor name must match class name
• Adding return type (invalid in constructors)
• Not using constructors for initialization
• Overcomplicating constructor logic

9. Best Practices

• Keep constructors simple
• Use parameterized constructors for required values
• Avoid heavy logic inside constructors
• Use overloading when multiple initialization options are needed

Contents related to 'C# Constructors Explained: Types, Examples and Usage Guide'

C# Classes and Objects: OOP Basics Explained with Examples
C# Classes and Objects: OOP Basics Explained with Examples
C# Inheritance Explained: OOP Concept with Examples
C# Inheritance Explained: OOP Concept with Examples