Copy constructor means one of constructor of class takes object as a parameter of same class.
Example:
class customer
{
public int Cid { get; set; }
public string Cname { get; set; }
public customer()
{
}
public customer(int id,string name)
{
this.Cid = id;
this.Cname = name;
}
public customer(customer cobj)
{
this.Cid = cobj.Cid;
this.Cname = cobj.Cname;
}
static void Main()
{
customer cobj1 = new customer();
cobj1.Cid = 105;
cobj1.Cname = "narayanarao";
Console.WriteLine(cobj1.Cid);
Console.WriteLine(cobj1.Cname);
customer cobs = new customer(cobj1);
Console.WriteLine("===copy constructor====");
Console.WriteLine(cobs.Cid);
Console.WriteLine(cobs.Cname);
Console.Read();
}
}
In this case copy constructor doesn’t execute again the code. Which copies the data form existing object.or your passing object.
No comments:
Post a Comment