Algorithm
Constructor overloading in C# allows you to create multiple constructors for a class, each with a different set of parameters. This enables you to instantiate objects in various ways, providing flexibility and ease of use. Here's an example to illustrate constructor overloading:
Code Examples
#1 Different number of parameters in C# Programming
Code -
C# Programming
class Car {
Car() {
...
}
Car(string brand) {
...
}
Car(string brand, int price) {
...
}
}
Copy The Code &
Try With Live Editor
#2 Code Example- Constructor Overloading with different number of parameter in C#
Code -
C# Programming
using System;
namespace ConstructorOverload {
class Car {
// constructor with no parameter
Car() {
Console.WriteLine("Car constructor");
}
// constructor with one parameter
Car(string brand) {
Console.WriteLine("Car constructor with one parameter");
Console.WriteLine("Brand: " + brand);
}
static void Main(string[] args) {
// call with no parameter
Car car = new Car();
Console.WriteLine();
// call with one parameter
Car car2 = new Car("Bugatti");
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Car constructor with one parameter
Brand: Bugatti
#3 Different types of parameters in C#
Code -
C# Programming
class Car {
Car(string brand) {
...
}
Car(int price) {
...
}
}
Copy The Code &
Try With Live Editor
#4 Constructor overloading with different types of parameters in C# Programming
Code -
C# Programming
using System;
namespace ConstructorOverload {
class Car {
// constructor with string parameter
Car(string brand) {
Console.WriteLine("Brand: " + brand);
}
// constructor with int parameter
Car(int price) {
Console.WriteLine("Price: " + price);
}
static void Main(string[] args) {
// call constructor with string parameter
Car car = new Car("Lamborghini");
Console.WriteLine();
// call constructor with int parameter
Car car2 =new Car(50000);
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Price: 50000
#5 Different order of parameters in C#
Code -
C# Programming
Car {
Car(string brand, int price) {
...
}
Car(int speed, string color) {
...
}
}
Copy The Code &
Try With Live Editor
#6 Constructor overloading with different order of parameters in C# Programming
Code -
C# Programming
using System;
namespace ConstructorOverload {
class Car {
// constructor with string and int parameter
Car(string brand, int price) {
Console.WriteLine("Brand: " + brand);
Console.WriteLine("Price: " + price);
}
// constructor with int and string parameter
Car(int speed, string color) {
Console.WriteLine("Speed: " + speed + " km/hr");
Console.WriteLine("Color: " + color);
}
static void Main(string[] args) {
// call constructor with string and int parameter
Car car = new Car("Bugatti", 50000);
Console.WriteLine();
// call constructor with int and string parameter
Car car2 =new Car(60, "Red");
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Price: 50000
Speed: 60 km/hr
Color: Red
Demonstration
C# Programming Constructor Overloading