Write a code snippet to show copy constructor?

Tripati_tutu
Posted by Tripati_tutu under C# category on | Points: 40 | Views : 2984
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopyConst
{
class Program
{
int x, y;
public Program(int xx, int yy)
{
x = xx;
y = yy;
}
public Program(Program p)
{
x = p.x;
y = p.y; // Copy constructor
}
static void Main(string[] args)
{
Program obj1 = new Program(10, 20);
Console.WriteLine(obj1.x + " " + obj1.y);
Program obj2 = new Program(obj1);
Console.WriteLine(obj2.x + " " + obj2.y);
obj1.x = 50;
obj1.y = 70;
Console.WriteLine(obj1.x + " " + obj1.y);
Console.WriteLine(obj2.x + " " + obj2.y);

Console.ReadKey();
}
}
}


Since the above example consist of two constructors, so we can confirm that the constructor can be overloadable.

Comments or Responses

Login to post response