Introduction:

In this article we are going to learn about Copy Constructor what is Copy Constructor  or how to use Copy Constructor or why use Copy Constructor in c# with example.

Description:

In c#, Copy Constructor is a parameterized constructor which contains a parameter of 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 copy constructor is a constructor which copies a data of one object into another object. Generally, c# won’t provide a copy constructor for objects but we can implement ourselves based on our requirements.

C# Copy Constructor Syntax

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

 

class User
    {
        public string name, address;
        // Parameterized Constructor
        public User(string a, string b)
        {
            name = a;
            address = b;
        }
 
        // Copy Constructor
        public User(User user)
        {
            name = user.name;
            address = user.address;
        }
    }

 

If you observe above syntax, we created a copy constructor with a parameter of same class type and it help us to 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 c# programming language.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLTutorialsPoint
{
    class User
    {
        public string name, address;
        // Parameterized Constructor
        public User(string a, string b)
        {
            name = a;
            address = b;
        }
 
        // Copy Constructor
        public User(User user)
        {
            name = user.name;
            address = user.address;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // User object with Parameterized constructor
            User user = new User("Aditya Kumar", "Allahabad");
 
            // Another User object (user1) by copying user details
            User user1 = new User(user);
            user1.name = "Pankaj Patel";
            user1.address = "Allahabad";
            Console.WriteLine(user.name + ", " + user.address);
            Console.WriteLine(user1.name + ", " + user1.address);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}
 

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

 

When we execute above c# program, we will get the result like as shown below:



 C# Copy Constructor with Example

 

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

 

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


I hope ti will help to you after reading.


0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like