Copy Constructors – C Sharp example

Copy Constructors C# example – A class can create multiple objects. A copy constructor copies the variables from another object.

The purpose of a copy constructor is to initialize a new object to the values of an existing object.

Syntax

public Car(Car marutiWithPrice)// declaring Copy constructor
        {
           Console.WriteLine("Copy constructor is called");
            this.name = marutiWithPrice.name;
            this.price = marutiWithPrice.price;
        } 

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

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

        public Car(Car marutiWithPrice)// declaring Copy constructor
        {
            Console.WriteLine("Copy constructor is called");
            this.name = marutiWithPrice.name;
            this.price = marutiWithPrice.price;
        }

        // method
        public void run()
        {
            Console.WriteLine(name + " Car is running...");
        }
    }

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

For example,

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).

If you want to copy the values of Maruti object to another object say honda, then we can give the reference of maruti object to a honda object. for example
Car honda = new Car(Maruti ); //2 arguments i.e. String and double arguments of maruti are passed as reference during creation of object.

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

  /*
* Copy constructors C# example
*/
    class Car
    {
        String name;
        double price;        
            
        // Constructor with 2 parameters
        public Car(String name, double price)// declaring Instance constructor
        {
            this.name = name;
            this.price = price;
            Console.WriteLine("Instance constructor is called");
            Console.WriteLine("Car name :" + this.name + ", price:" + this.price);
        }

        public Car(Car marutiWithPrice)// declaring Copy constructor
        {
            // TODO: Complete member initialization

            Console.WriteLine("Copy constructor is called");
            name = marutiWithPrice.name;
            price = marutiWithPrice.price;
            Console.WriteLine("Car name :" + name + ", price:" + price);
        }

        // method
        public void run()
        {
            Console.WriteLine(name + " Car is running...");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            // Create object with 2 parameter Car(String name,double price))
            // constructor will be called automatically.
          
            // Test 1 parameters constructor
            Car marutiWithPrice = new Car("Maruti", 400000.00);//// Create a new Car object.
            marutiWithPrice.run();

            Car hondaWithPrice = new Car(marutiWithPrice);//here is marutiWithPrice details is copied to hondaWithPrice.
            hondaWithPrice.run();
        }
    }

Output
Instance constructor is called
Car name :Maruti, price:400000
Maruti Car is running…
Copy constructor is called
Car name :Maruti, price:400000
Maruti Car is running…

Related Posts