Parameterized Constructor – C Sharp example

Parameterized constructor C# Example – Class constructors can also have parameters and receives arguments, the same way as class methods have.

For example, in below Car class, a parameterized constructor “public Car(String name)” is defined, that takes 1 string argument.

class Car {
	String name;	
	
	//parameterized constructor
	public Car(String name) {
		this.name = name;
	}
	
	void run(){
		Console.WriteLine(name+" Car is running...");
	}
}

Read previous simple C# constructor example first in which, we have empty constructor e.g. Car() which was invoked on creating object like Car Maruti = new Car();

In this example,

Now lets say we want to give name to cars, for example Maruti and Honda etc. So how can we do that. in previous example we have not given name.

we know that when we create an object of the class the constructor get called automatically.

so, we can create a parameterized constructor where we can set the name of the car.

This program now prints the output as below

Maruti Car is running…
honda Car is running…

Where as in previous example output was

Constructor…
Car is running…
Constructor…
Car is running…

C# Code – Parameterized constructor C# example

  /*
 * parameterized constructor C# example
 */

    class Car
    {
        String name;

        //parameterized constructor
        public Car(String name)
        {
            this.name = name;
        }

    public void run()
        {
            Console.WriteLine(name + " Car is running...");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //We we create an object, the constructor of the 
            //class will be called automatically.
            Car maruti = new Car("Maruti");
            maruti.run();

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

Multiple parameters in constructor in C#

A constructor can have multiple number of arguments. For example, in below Car class there are two arguments i.e. public Car(String name,String color)

When you create object of class with two arguments like below, it will automatically call the constructor.

Car maruti = new Car(“Maruti”,”red”);

C# Code

 /*
 * Example of Multiple parameters in constructor in C#
 */

    class Car
    {
        String name;
        String color;

        // parameterized constructor
        public Car(String name, String color)
        {
            this.name = name;
            this.color = color;
        }

      public  void run() {
            Console.WriteLine(color + " " + name + " " + " Car is running...");
	}
    }
    
    
    class Program
    {
        static void Main(string[] args)
        {// We we create an object, the constructor of the
            // class will be called automatically.
            Car maruti = new Car("Maruti", "Red");
            maruti.run();

            Car honda = new Car("Honda", "Black");
            honda.run();
        }
    }

Output:
Red Maruti Car is running…
Black Honda Car is running…

Related Posts