You must Sign In to post a response.
  • Category: .NET

    What is Copy constructor ?

    Are you looking for a way to know more about copy constructor ? then read this thread to know what is copy constructor

    Sir can explain about copy consturctor in detail.. and also i want to know like in copy constructo can we copy object data from one method to another method.

    Can explain about copy constructo in detail
  • #743494

    The copy constructor creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to:

    1.Initialize one object from another of the same type.

    2.Copy an object to pass it as an argument to a function.

    3.Copy an object to return it from a function.

    sample :

    classname (const classname &obj) {
    // body of constructor

  • #743510
    First thing we need to note is, C# doesn't provide a copy constructor for objects, but you can write one yourself. it is basically used to creating a new object as a copy of an existing object.
    see below sample

    //see below sample class
    class example
    example(const example&)
    // this is the copy constructor
    //now in main method, creating a new object as a copy of an existing object. it will called automatically

    int main(void)
    example obj1; //normal constructor called
    example obj2 = obj1; // copy constructor called

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #743516
    Refer this msdn article on the Copy Constructor.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #743531

    Refer the below link,There is well description of contructors as well as copy constructor with examples

    Hope this helps you

  • #743627
    you will find so simple and nice explanation in MSDN for copy constructor. Please read it :

    Asheej T K

  • #743646

    Copy Constructor

    The constructor which creates an object by copying variables from another object is called a copy constructor. The purpose of a copy constructor is to initialize a new instance to the values of an existing instance.


    public employee(employee emp)

    The copy constructor is invoked by instantiating an object of type employee and passing it the object to be copied.


    employee emp1=new employee (emp2);
    Now, emp1 is a copy of emp2.

    So let us see its practical implementation.

    using System;
    namespace copyConstractor
    class employee
    private string name;
    private int age;
    public employee(employee emp) // declaring Copy constructor.
    name =;
    age = emp.age;
    public employee(string name, int age) // Instance constructor.
    { = name;
    this.age = age;
    public string Details // Get deatils of employee
    return " The age of " + name +" is "+ age.ToString();
    class empdetail
    static void Main()
    { employee emp1 = new employee("Vithal", 23); // Create a new employee object.
    employee emp2 = new employee(emp1); // here is emp1 details is copied to emp2.

  • #744560
    Copy constructor--

    A parameterized constructor that contains a parameter of same class type is called as copy constructor.
    Main purpose of copy constructor is to initialize new instance to the values of an existing instance.
    Check below example

    class Member
    // Copy constructor.
    public Member(Member previousPerson)
    Name = previousPerson.Name;
    Age = previousPerson.Age;

    // Instance constructor.
    public Member(string name, int age)
    Name = name;
    Age = age;

    public int Age { get; set; }

    public string Name { get; set; }

    public string Details()
    return Name + " is " + Age.ToString();

    class TestPerson
    static void Main()

    Member person1 = new Member("dipti", 40);

    Member person2 = new Member(person1);

    // Change each person's age.
    person1.Age = 39;
    person2.Age = 41;

    // Change person2's name.
    person2.Name = "megha";

    // Show details to verify that the name and age fields are distinct.

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    // Output:
    // dipti is 39
    // megha is 41


    Dipti Choudhari

  • Sign In to post your comments