Tuesday, July 7, 2009

Copy Constructor

Define:

Constructor will initialize only one at a time while coping the objects. New instance will not create. it will refer the existing class.

Copy constructor will not provide c# directly. But we can copy the objects using methods.

let me explain further with example.

public string var1 = string.Empty;
public string var2 = string.Empty;

//Copy Constructor
public CopyConstructor(CopyConstructor cpyCons)
{
var1 = cpyCons.var1;
var2 = cpyCons.var2;
}
//Instance of constructor
public CopyConstructor()
{
this.var1 = "s";
this.var2 = "s1";
}

CopyConstructor ss = new CopyConstructor();

CopyConstructor ss1 = new CopyConstructor(ss); //Coping the class using copy constructor.