Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does the interface ICloneable do in C#?
The ICloneable interface in C# provides a mechanism to create a copy of an existing object, known as cloning. This interface is part of the System namespace and defines a standard way to duplicate objects.
Syntax
The ICloneable interface contains only one method −
public interface ICloneable {
object Clone();
}
To implement ICloneable, a class must provide the Clone() method −
public class MyClass : ICloneable {
public object Clone() {
// return a copy of the current object
}
}
Clone() Method
The Clone() method creates a new object that is a copy of the current instance. However, the interface doesn't specify whether the cloning should be shallow or deep, leaving this decision to the implementer.
Using ICloneable for Simple Objects
Example
using System;
class Car : ICloneable {
int width;
public Car(int width) {
this.width = width;
}
public object Clone() {
return new Car(this.width);
}
public override string ToString() {
return string.Format("Width of car = {0}", this.width);
}
}
class Program {
static void Main() {
Car carOne = new Car(1695);
Car carTwo = carOne.Clone() as Car;
Console.WriteLine("{0}mm", carOne);
Console.WriteLine("{0}mm", carTwo);
}
}
The output of the above code is −
Width of car = 1695mm Width of car = 1695mm
Using MemberwiseClone for Shallow Copying
Example
using System;
class Person : ICloneable {
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age) {
Name = name;
Age = age;
}
public object Clone() {
return this.MemberwiseClone();
}
public override string ToString() {
return $"Name: {Name}, Age: {Age}";
}
}
class Program {
static void Main() {
Person original = new Person("Alice", 25);
Person cloned = (Person)original.Clone();
Console.WriteLine("Original: " + original);
Console.WriteLine("Cloned: " + cloned);
cloned.Name = "Bob";
cloned.Age = 30;
Console.WriteLine("After modification:");
Console.WriteLine("Original: " + original);
Console.WriteLine("Cloned: " + cloned);
}
}
The output of the above code is −
Original: Name: Alice, Age: 25 Cloned: Name: Alice, Age: 25 After modification: Original: Name: Alice, Age: 25 Cloned: Name: Bob, Age: 30
Key Considerations
| Aspect | Description |
|---|---|
| Return Type | Returns object, requires casting to actual type |
| Clone Depth | Interface doesn't specify shallow vs deep cloning |
| Thread Safety | Implementation must handle thread safety if needed |
| Performance | Shallow cloning is faster than deep cloning |
Conclusion
The ICloneable interface provides a standard way to create object copies in C#. While simple to implement, developers must decide between shallow and deep cloning based on their specific needs, with MemberwiseClone() providing an easy shallow copy solution for most scenarios.
