top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Copy Constructor in C#

+1 vote
313 views

Copy constructor is used to initialize object from another object. Instead of the concept for copy constructor we can use assignment operation to initialize the object. Let’s suppose we are having 1 object containing 1000 variable and after calculation values are assigned to each of the variable. Now we need to copy this variable values to another object. If we use assignment operator then program will become too large and hence we required special type of constructor to initialize the object which is called as Copy of Constructor.

In copy of constructor we use Overloading concept. In this we can create two constructor with two different parameter.

Overloading: This concept is useful when we need the function / method of the same name but different signature or parameter or type. It is the example of compile time polymorphism. 

As per our requirement we can use this concept in different way.

Example 1: I have written a small program to use this concept.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace CopyConsrtuctor

{

    class Consrtuctorcopy

    {

        public string str1,str2,str3;

        public Consrtuctorcopy(string x, string y)

        {

            str1 = x;

            str2 = y;

            str3 = "Testing of objectcon";

        }

        public Consrtuctorcopy(Consrtuctorcopy copyobj)

        {

            str1 = copyobj.str1;

            str2 = copyobj.str2;

            str3 = str2 + " "+ str1;

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Consrtuctorcopy objcon = new Consrtuctorcopy ("Test", "pass"); 

            Consrtuctorcopy objcopy = new Consrtuctorcopy (objcon);

            Console.WriteLine(objcopy.str1 + " to " + objcopy.str2 + "\n"+ objcopy.str3 +"\n"+objcon.str3);

            Console.ReadLine();

        }

    }

}

Working:

In the main program we have created an object(objcon) of the class(Consrtuctorcopy) the pass the parameter.

When object of the constructor was created objcon then value objcon.str1 and objcon.str2 initialize and we get value of objcon .str3 with the help of str1 and str2. And values are initialize during compile time.

Now we have created another object objcopy and pass the parameter as object. Then initialization of this object will be done with object objcon as shown in the above example.

This method is called as passing an object by value.

 

Example 2:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsoleApplication3

{

    class Sample

    {

        public string str1,str2,str3;

        public Sample(string x, string y)

        {

            str1 = x;

            str2 = y;

            str3 = "Testing of objectcon";

        }

        public Sample(Sample obj)     // Copy Constructor

        {

            str1 = obj.str1+"Test";

            str2 = obj.str2;

            str3 = str2 + " "+ str1;

           

        }

        public void sample()

        {

          

            str3 = str2 + " " + str1;

            Console.WriteLine(str3);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Sample objcon = new Sample("Test", "pass"); 

           

            objcon.sample();

 

 

            Console.WriteLine(objcon.str1 + " to " + objcon.str2 + "\n" + objcon.str3);

            Console.ReadLine();

        }

    }

}

 

This method is called as initializing constructor value by object value.

 

posted Nov 15, 2016 by Barbara Jones

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...