C# Constructors Overloading

Muhammad Zeeshan Oct 12, 2023
  1. Constructors in C#
  2. Constructor Overloading in C#
  3. Ways of Constructor Overloading
C# Constructors Overloading

Constructor Overloading is a topic covered in this article in C#. To get started, the first thing we need to do is get an understanding of what constructors are in C#.

Constructors in C#

A constructor is a particular class method that is called whenever an instance of that class is produced. This method is automatically triggered when an instance of the class is created.

The constructor’s primary function is to initialize the class’s private fields before constructing an instance of the class. This happens concurrently with the creation of the instance.

If you do not manually build a constructor for the class, the compiler will do it for you and name it the default constructor of the class. The zero value is assigned to all the class’s numeric fields, and the " " or Null value is assigned to all the string classes and object fields when the default constructor is used.

Constructor Overloading in C#

Creating numerous constructors, each with a unique combination of arguments and a varying quantity of those parameters is called constructor overloading. It enables us to utilize a class differently than we normally would.

The overloading of a class’s constructors might cause the same class to act differently depending on the context. When the object is initialized first, it may only display a basic text message.

Still, it might do some mathematical computation when initialized a second time with a different argument. Overloading constructors in C# is possible, just like overloading methods in other programming languages, and to make use of constructor overloading, two or more constructors must share the same name but have distinct functions.

Ways of Constructor Overloading

Constructor overloading is possible for us to carry out in several ways, for example, with different types of parameters, different numbers of parameters, and different order of parameters.

With Different Types of Parameters

For this procedure, we will design two phones constructors, each of which will have the same arguments. In this case, we can generate constructors with the same arguments since the data type included inside the parameters differs.

In the class Phone, we have constructed two constructors with the same name but which take data types distinct from one another. The first constructor accepts a string for its argument, whereas the other accepts integers.

class Phone {
  Phone(string pbrand) {
    Console.WriteLine("Phone Brand is: " + pbrand);
  }
  Phone(int pprice) {
    Console.WriteLine("Phone Price is: " + pprice);
  }

We’ve overloaded the constructor in the main method with various arguments. In this case, Object p calls the constructor with a string type parameter, while Object p1 calls the constructor with an argument of the int type.

static void Main(string[] args) {
  Phone p = new Phone("iPhone x ");
  Console.WriteLine();

  Phone p1 = new Phone(10000);
  Console.ReadLine();
}
}

Complete Source Code:

using System;

namespace Shanii {
  class Phone {
    Phone(string pbrand) {
      Console.WriteLine("Phone Brand is: " + pbrand);
    }
    Phone(int pprice) {
      Console.WriteLine("Phone Price is: " + pprice);
    }

    static void Main(string[] args) {
      Phone p = new Phone("iPhone x ");
      Console.WriteLine();

      Phone p1 = new Phone(10000);
      Console.ReadLine();
    }
  }
}

Output:

Phone Brand is: iPhone x
Phone Price is: 10000

With Different Numbers of Parameter

The following example shows that the Phone constructor has two constructors, one with no parameters and the other with one.

We created two Phone constructors with the same name in the Phone class. One does not require any parameters, whereas the other requires one parameter.

class Phone {
  Phone() {
    Console.WriteLine("A Phone Constructor with no parameter");
  }
  Phone(string pbrand) {
    Console.WriteLine("A Phone constructor with one parameter");
    Console.WriteLine("Phone Brand: " + pbrand);
  }
}

Now that we’ve created constructors, we’ll use them in the Main() function to call their respective methods. As you can see, both objects p and p1 call constructors with the same name.

The relevant constructor is invoked based on the value of the parameter supplied to the constructor.

static void Main(string[] args) {
  Phone p = new Phone();
  Console.WriteLine();

  Phone p1 = new Phone("Samsung S22 ultra");
  Console.ReadLine();
}

Complete Source Code:

using System;
namespace Shanii {

  class Phone {
    Phone() {
      Console.WriteLine("A Phone Constructor with no parameter");
    }
    Phone(string pbrand) {
      Console.WriteLine("A Phone constructor with one parameter");
      Console.WriteLine("Phone Brand: " + pbrand);
    }

    static void Main(string[] args) {
      Phone p = new Phone();
      Console.WriteLine();

      Phone p1 = new Phone("Samsung S22 ultra");
      Console.ReadLine();
    }
  }
}

Output:

A Phone Constructor with no parameter

A Phone constructor with one parameter
Phone Brand: Samsung S22 ultra

With Different Order of Parameter

There are two different constructors here, each of which has the same amount of arguments. This is feasible because the order of data type in parameters may take various forms.

In the first Phone constructor, the string data type comes before the int data type, whereas in the second Phone constructor, the int data type comes before the string data type, which makes a difference between both of the Phone constructors.

Phone(string pbrand, int pprice) {
  Console.WriteLine("Phone Brand: " + pbrand);
  Console.WriteLine("Phone Price: " + pprice);
}
Phone(int Andriodversion, string pbrand) {
  Console.WriteLine("Phone Andriod Version: " + Andriodversion);
  Console.WriteLine("Phone Brand: " + pbrand);
}

We have overloaded the constructors by passing the arguments in various ordering in the Main() function.

static void Main(string[] args) {
  Phone p = new Phone("Samsung Galaxy s22", 10000);
  Console.WriteLine();

  Phone p1 = new Phone(15, " iPhone 13 pro");
  Console.ReadLine();
}

Complete Source Code:

using System;
namespace Shanii {
  class Phone {
    Phone(string pbrand, int pprice) {
      Console.WriteLine("Phone Brand: " + pbrand);
      Console.WriteLine("Phone Price: " + pprice);
    }
    Phone(int Andriodversion, string pbrand) {
      Console.WriteLine("Phone Andriod Version: " + Andriodversion);
      Console.WriteLine("Phone Brand: " + pbrand);
    }
    static void Main(string[] args) {
      Phone p = new Phone("Samsung Galaxy s22", 10000);
      Console.WriteLine();

      Phone p1 = new Phone(15, " iPhone 13 pro");
      Console.ReadLine();
    }
  }
}

Output:

Phone Brand: Samsung Galaxy s22
Phone Price: 10000

Phone Andriod Version: 15
Phone Brand:  iPhone 13 pro
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn