in Technology by
What is Copy Constructor in C#?

1 Answer

0 votes
by

Copy Constructor

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);

Related questions

0 votes
    I believe I came across this a couple months ago, but now I'm having trouble finding the answer. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    What do you understand by copy constructor in Java?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    What is Default Constructor in C#?...
asked Jan 1, 2021 in Technology by JackTerrance
0 votes
    How do I implement a Copy menu item in a Windows application written in C#/.NET 2.0? I want to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    Can an abstract class have a constructor?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    What is the difference between a constructor and a method?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    If we need to inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how can we do that?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    What should be the execution order, if a class has a method, static block, instance block, and ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    I'm trying to create two dimensional std::vector, that will hold objects of this class I inherited. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    What are the best practices if you have a class which accepts some parameters but none of them are allowed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 5, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
...