1. OnlineBuff

Understand Private Constructor and Copy Constructor with code sample

What is a Copy Constructor?

C# does not provide a copy constructor. A copy constructor enables you to copy the data stored in the member variables of an object of the class into another object.

In simple words a copy constructor means a constructor which copies data of one object into another object,

Example of Copy Constructor

Class Car{ 

      //Instance Constructor
        public Car(string nameofcar, int carno, double carprice)
            _NameofCar = nameofcar;
            _CarNo = carno;
            _CarPrice = carprice;

        //Copy Constructor
        public Car(Car objCar)
            _NameofCar = objCar._NameofCar;
            _CarNo = objCar._CarNo;
            _CarPrice = objCar._CarPrice;

Above the class "Car" has two types of constructor. The first constructor is an Instance Constructor of a class with three input parameters and second is a copy constructor with input parameter of a class object.

When we assign some input values to class properties using instance constructor then with the use of copy constructor we can create copy of data stored in an object to another new object as shown below code snippet.

static void Main(string[] args)
Console.WriteLine("Print Car Details From First Instance Object \n");
Car objCar = new Car("BMW Sedan", 12345, 454555.45); 
Console.WriteLine("Name of a Car --> {0}",objCar._NameofCar);
Console.WriteLine("Car Plate Number --> {0}", objCar._CarNo);
Console.WriteLine("Price of a Car --> {0}", objCar._CarPrice);
Console.WriteLine("------------------------------------- \n");
Console.WriteLine("Print Car Details From Copy Constructor \n");
Car objCar2 = new Car(objCar);//Copy Data Stored in Objcar
Console.WriteLine("Name of a Car --> {0}", objCar2._NameofCar);
Console.WriteLine("Car Plate Number --> {0}", objCar2._CarNo);
Console.WriteLine("Price of a Car --> {0}", objCar2._CarPrice);  

Above the code shows that we have created class object "objCar" with three input parameters using instance constructor means "objCar" object now holds three values as shown in code.

Now next step we have created another object "objCar2" with class object "objCar" using copy constructor. So now object "objCar2" automatically copies all data stored in "objCar" to "objCar2".

When to use of Copy Constructor

Audit Class
To remember the old values of a class

What is a Private Constructor?

Private constructor a constructor with "private" access modifier in a class.
A class with private constructor cannot be inherited.
We cannot create an object of the class which has private constructor.
A long with private constructor we can also have public constructor (overloaded constructor).
To access the methods or properties in a class with private constructor we need to assign methods or properties with "static" keyword.

Static: .NET runtime environment call static method of the class where main is defined. By making Main method as static .net runtime environment do not need to make object of the class.

A sample code example of a class "EmployeeVisitCounter" with a private constructor


public class EmployeeVisitCounter
        private EmployeeVisitCounter()
		…..Private Constructor
        public static int currentCount;
        public static int CountEmployeeVisited()
            return ++currentCount;

        public static string getVisitedCounter()
            return "Hello Employee visit page views is --> " + currentCount;

Above code illustrates a class "EmployeeVisitCounter" with a private constructor and with static properties like CountEmployeeVisited (),getVisitedCounter().

We made all property static because we cannot able to create instance of a call due to its protection level of private constructor. Static keyword is must to call methods or properties of a private constructor.

Now next step let's call class "EmployeeVisitCounter" in our main method as shown below.

  static void Main(string[] args)
            EmployeeVisitCounter.currentCount = 1000;
            EmployeeVisitCounter.CountEmployeeVisited ();

As you can see from above code that without creating an instance or object of class and with protection level of private constructor we are able to see output of the "getVisitedCounter()" i.e. 1001.

When to use of Private Constructor

To Create Helper Class
To Create Common Routine Function class
To Create Utilities

Author: Gurunatha Dogi

is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more. Follow me @ and twitter.... read more


By Ankit on 2014-05-17
help .net or oracle

Add a Comment