What is a copy constructor in C#?

A copy constructor in C# creates a new object by copying variables from another object of the same class. It provides a way to initialize a new object with the values of an existing object, creating a separate copy rather than a reference to the original object.

Syntax

Following is the syntax for creating a copy constructor −

public ClassName(ClassName obj) {
   this.field1 = obj.field1;
   this.field2 = obj.field2;
   // copy other fields
}

How Copy Constructor Works

A copy constructor takes an object of the same class as a parameter and copies its field values to the new object being created. This ensures that both objects have identical data but exist as separate instances in memory.

Copy Constructor Process Original Object name: "Jack" rank: 2 New Object name: "Jack" rank: 2 Copy Values Same data, different memory locations

Example

Here's a complete example demonstrating the copy constructor −

using System;

class Student {
   private string name;
   private int rank;

   public Student(Student s) {
      name = s.name;
      rank = s.rank;
   }

   public Student(string name, int rank) {
      this.name = name;
      this.rank = rank;
   }

   public string Display {
      get {
         return " Student " + name +" got Rank "+ rank.ToString();
      }
   }
}

class StudentInfo {
   static void Main() {
      Student s1 = new Student("Jack", 2);

      // copy constructor
      Student s2 = new Student(s1);

      // display
      Console.WriteLine(s2.Display);
   }
}

The output of the above code is −

 Student Jack got Rank 2

Copy Constructor vs Object Reference

Understanding the difference between copy constructor and simple object assignment is crucial −

using System;

class Person {
   public string name;
   
   public Person(string n) {
      name = n;
   }
   
   public Person(Person p) {
      name = p.name;
   }
}

class Program {
   static void Main() {
      Person p1 = new Person("Alice");
      
      // Using copy constructor - creates new object
      Person p2 = new Person(p1);
      
      // Simple assignment - creates reference
      Person p3 = p1;
      
      p1.name = "Bob";
      
      Console.WriteLine("p1.name: " + p1.name);
      Console.WriteLine("p2.name: " + p2.name);
      Console.WriteLine("p3.name: " + p3.name);
   }
}

The output of the above code is −

p1.name: Bob
p2.name: Alice
p3.name: Bob

Key Benefits

  • Independent Objects: Creates a new object with copied values, not a reference.

  • Data Safety: Modifications to one object don't affect the copied object.

  • Controlled Copying: You can customize which fields to copy and how to copy them.

  • Deep Copy Support: Useful for creating deep copies of complex objects.

Conclusion

Copy constructors in C# provide a clean way to create new objects by copying data from existing objects. They ensure that the new object is independent from the original, making them essential for scenarios where you need separate instances with identical data values.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements