Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does the interface ICloneable do in C#?
The ICloneable interface creates a copy of the exisiting object i.e a clone.
It only has a single method −
Clone() − The clone() method creates a new object that is a copy of the current instance.
The following is an example showing how to perform cloning using Icloneable interface −
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);
}
}
Output
Width of car = 1695mm Width of car = 1695mm
Advertisements