C# Copy Constructor with Examples

In c#, Copy Constructor is a parameterized constructor that contains a parameter of the same class type. The copy constructor in c# is useful whenever we want to initialize a new instance to the values of an existing instance.

 

In simple words, we can say a copy constructor is a constructor that copies one object's data into another object. Generally, c# won’t provide a copy constructor for objects, but we can implement it ourselves based on our requirements.

C# Copy Constructor Syntax

Following is the syntax of defining a copy constructor in the c# programming language.

 

class User
{
     // Parameterized Constructor
     public User(string a, string b)
     {
         // your code
     }
     // Copy Constructor
     public User(User user) {
        // your code
     }
}

If you observe the above syntax, we created a copy constructor with a parameter of the same class type, and it helps us initialize a new instance to the values of an existing instance.

C# Copy Constructor Example

Following is the example of creating a copy constructor to initialize a new instance to the values of an existing instance in the c# programming language.

 

using System;

namespace Tutlane
{
     class User
     {
        public string name, location;
        // Parameterized Constructor
        public User(string a, string b)
        {
            name = a;
            location = b;
        }
        // Copy Constructor
        public User(User user)
        {
            name = user.name;
            location = user.location;
        }
     }
     class Program
     {
        static void Main(string[] args)
        {
            // User object with a Parameterized constructor
            User user = new User("Suresh Dasari", "Hyderabad");
            // Another User object (user1) by copying user details
            User user1 = new User(user);
            user1.name = "Rohini Alavala";
            user1.location = "Guntur";
            Console.WriteLine(user.name + ", " + user.location);
            Console.WriteLine(user1.name + ", " + user1.location);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
     }
}

If you observe the above example, we created an instance of copy constructor (user1) and used an instance of user object as a parameter type. So the properties of the user object will be sent to the user1 object, and we are changing the property values of the user1 object, but those will not affect the user object property values.

 

When you execute the above c# program, you will get the result below.

 

C# Copy Constructor Example Result

 

If you observe the above result, we initialized a new instance to the values of an existing instance, but those changes did not effect the existing instance values.

 

This is how you can use a copy constructor in the c# programming language to create a constructor with the parameter of the same class type based on our requirements.