ICloneable इंटरफ़ेस मौजूदा ऑब्जेक्ट की एक कॉपी बनाता है यानी एक क्लोन।
इसमें केवल एक ही मेथड है:
Clone() - Clone() मेथड एक नया ऑब्जेक्ट बनाता है जो वर्तमान इंस्टेंस की एक कॉपी है।
निम्नलिखित एक उदाहरण दिखा रहा है कि ICloneable इंटरफ़ेस का उपयोग करके क्लोनिंग कैसे करें:
उदाहरण
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); } }आउटपुट
Width of car = 1695mm Width of car = 1695mm