Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Clone() ใน C#

เมธอด Clone() ใน C# ใช้สร้างสำเนาที่คล้ายกันของอาร์เรย์ (shallow copy)

ตัวอย่าง

using System;

class Program {
    static void Main() {
        string[] arr = { "one", "two", "three", "four", "five" };
        string[] arrCloned = arr.Clone() as string[];
        Console.WriteLine(string.Join(",", arr));
        Console.WriteLine(string.Join(",", arrCloned));
        Console.WriteLine();
    }
}

ผลลัพธ์

one,two,three,four,five
one,two,three,four,five

ใช้ arr.Clone() as string[] เพื่อโคลนอาร์เรย์สตริง