Multiple Constructors – C Sharp example

Multiple constructors C# example – A class can have multiple constructors with different types of arguments and different number of arguments.

For example, in below Car class we have three constructors written for Car class i.e. with empty, one parameter and two parameters.

class Car {
	String name;
	double price;
 
	// Empty constructor
	public Car() {
	}
 
	// Constructor with 1 parameter
	public Car(String name) {
	}
 
	// Constructor with 2 parameters
	public Car(String name, double price) {
	}
 
	// method
	void run() {
	}
}

We know that when a class object is created, the constructor is called automatically. Have a look on this constructor example in C# , where on creation of object of class Car, the Car constructor is automatically invoked. Also, recommend to read Simple parametrized constructor in C# example. for better understanding.

When we create class object with parameters then the matching constructors get called.

For example,

If you create an object of Car with 1 argument, the constructor having single parameter will be called automatically. For example

Car Maruti = new Car(“maruti); // 1 String argument is passed during creation of object.

So, it will look for a constructor with 1 string parameter i.e. public Car(String name)

Similarly, if you create object with 2 arguments, the constructor having 2 parameters will be called automatically. for example,

Car Maruti = new Car(“Maruti”, double price); // 2 arguments i.e. String and double arguments are passed during creation of object.

So, it will look for a constructor with 2 parameters e.g. public Car(String name, double price).

Here is complete example that demonstrate the multiple constructors uses in C# program.

Multiple constructors Java example Source code

  /*
 * Multiple constructors C# example
 */
    class Car
    {
        String name;
        double price;

        // Constructor with 1 parameter
        public Car(String name) {
		this.name = name;
        Console.WriteLine("1 parameters constructor is called");
		Console.WriteLine("Car name :" + this.name);
	}

        // Constructor with 2 parameters
        public Car(String name, double price) {
		this.name = name;
		this.price = price;
        Console.WriteLine("2 parameters constructor is called");
		Console.WriteLine("Car name :" + this.name + ", price:" + this.price);
	}

        // method
       public void run() {
           Console.WriteLine(name + " Car is running...");
	}
    }
    
    
    class Program
    {
        static void Main(string[] args)
        {
            // Create object with 1 parameter Car(String name)
            // constructor will be called automatically.
            Car maruti = new Car("Maruti");
            maruti.run();

            Car honda = new Car("honda");
            honda.run();

            // Test 2 parameters constructor
            Car marutiWithPrice = new Car("Maruti", 400000.00);
            marutiWithPrice.run();

            Car hondaWithPrice = new Car("honda", 700000.00);
            hondaWithPrice.run();
        }
    }

Output:
1 parameters constructor is called
Car name :Maruti
Maruti Car is running…
1 parameters constructor is called
Car name :honda
honda Car is running…
2 parameters constructor is called
Car name :Maruti, price:400000.0
Maruti Car is running…
2 parameters constructor is called
Car name :honda, price:700000.0
honda Car is running…

EXERCISE:

Create a class named Pet. Create two constructors i.e. empty constructor and constructor with a parameter “name” of String type.

Solution:

class Pet {
 
	// Constructor
	Pet() {
 
	}
 
	//Constructor with parameter
	Pet(String name) {
 
	}
}

Related Posts