Now let's see an example of Copy Constructor. Copy constructor is the parameterized constructor which takes a parameter of the same type. It allows you to initialize a new object with the existing object values.
public class Person
{
private int m_PersonID;
private string m_FirstName, m_LastName, m_City;
public Person()
m_PersonID = 19929;
m_FirstName = "No First Name";
m_LastName = "No Last Name";
m_City = "No City";
}
public Person(string firstName,string lastName)
m_FirstName = firstName;
m_LastName = lastName;
//copy constructor
public Person(Person person)
m_PersonID = person.m_PersonID;
m_FirstName = person.m_FirstName;
m_LastName = person.m_LastName;
m_City = person.m_City;
Here’s an example:
// Instance constructor.
Person p1 = new Person(1, "DotNet", "Curry", "Pune");
// Copy Constructor
Person p2 = new Person(p1);