Constructor Overloading


In this article we are going to discuss about constructor overloading with examples in C#.

Constructor Overloading

Constructor overloading in C# allows a class to have multiple constructors with different signatures. This enables the creation of objects in different ways, providing flexibility in object initialization. Each overloaded constructor can perform different initializations based on the provided arguments.

Syntax

Here’s the basic syntax for constructor overloading.

class MyClass
{
    // Default constructor
    public MyClass()
    {
        // Initialization code
    }

    // Overloaded constructor with one parameter
    public MyClass(int param1)
    {
        // Initialization code using param1
    }

    // Overloaded constructor with two parameters
    public MyClass(int param1, string param2)
    {
        //Initialization code using param1 and param2
    }
}

Example

Let's create an example class that demonstrates constructor overloading.

using System;

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

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

    // Constructor with one parameter
    public Person(string name)
    {
        Name = name;
        Age = 0;
        Console.WriteLine("Constructor with one parameter called.");
    }

    // Constructor with two parameters
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
        Console.WriteLine("Constructor with two parameters called.");
    }

    public void DisplayInfo()
    {
        Console.WriteLine("Name:"+ Name +"," + "Age: "+ Age);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Using the default constructor
        Person person1 = new Person();
        person1.DisplayInfo();

        // Using the constructor with one parameter
        Person person2 = new Person("Rohatash");
        person2.DisplayInfo();

        // Using the constructor with two parameters
        Person person3 = new Person("Mohit", 30);
        person3.DisplayInfo();
    }
}

Output

Constructor Overloading in C#

When to Use Constructors Overloading

Constructor overloading is a powerful feature in C# that provides flexibility in object creation. Here are some scenarios where using constructor overloading is particularly beneficial.

1. Providing Multiple Initialization Options

When a class can be initialized in different ways, constructor overloading allows you to provide different constructors to accommodate various initialization scenarios.

Example

class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    // Default constructor
    public Point()
    {
        X = 0;
        Y = 0;
    }

    // Constructor with one parameter
    public Point(int x)
    {
        X = x;
        Y = 0;
    }

    // Constructor with two parameters
    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

2. Default Values and Optional Parameters

Constructor overloading can be used to set default values for parameters that are not always required.

Example

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

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

    // Constructor with one parameter
    public Employee(string name)
    {
        Name = name;
        Age = 0;
    }

    // Constructor with two parameters
    public Employee(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

Prev Next