เมธอด Stack.Clone() ใน C# ใช้สร้าง Shallow Copy ของ Stack คือ สร้าง Stack ใหม่ที่มีสมาชิกเหมือนกันทุกตัว (อ้างอิง object เดียวกันสำหรับ reference types) แต่เป็น instance แยกกัน
รูปแบบไวยากรณ์ (Syntax)
public virtual object Clone();
- คืนค่า : object (ต้อง cast กลับเป็น Stack)
- เป็น Shallow Copy - reference types จะอ้างอิง object เดียวกัน
ตัวอย่างที่ 1: Stack ของ int (Value Type)
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
stack.Push(150);
stack.Push(300);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
stack.Push(1250);
stack.Push(1500);
stack.Push(2000);
stack.Push(2500);
Console.WriteLine("Stack elements... (top to bottom)");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count = " + stack.Count);
stack.Push(3000);
stack.Push(3500);
stack.Push(4000);
Console.WriteLine("\nStack elements...updated");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("\nCount (updated) = " + stack.Count);
// Clone
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned");
foreach (int val in stack2)
{
Console.WriteLine(val);
}
Console.Write("Count of cloned stack = " + stack2.Count);
}
}
ผลลัพธ์
Stack elements... (top to bottom) 2500 2000 1500 1250 1000 750 500 300 150 Count = 9 Stack elements...updated 4000 3500 3000 2500 2000 1500 1250 1000 750 500 300 150 Count (updated) = 12 Stack elements...cloned 4000 3500 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of cloned stack = 12
ตัวอย่างที่ 2: Stack ของ string (Reference Type)
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements... (top to bottom)");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count = " + stack.Count);
stack.Push("Headphone");
stack.Push("Keyboard");
stack.Push("Earphone");
Console.WriteLine("\nStack elements...updated");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count (updated) = " + stack.Count);
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned");
foreach (string val in stack2)
{
Console.WriteLine(val);
}
Console.Write("Count of cloned stack = " + stack2.Count);
}
}
ผลลัพธ์
Stack elements... (top to bottom) Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count = 7 Stack elements...updated Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count (updated) = 10 Stack elements...cloned Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of cloned stack = 10
Shallow Copy vs Deep Copy
| ลักษณะ | Shallow Copy (Clone()) | Deep Copy |
|---|---|---|
| Value Types (int, struct) | คัดลอกค่าโดยสมบูรณ์ (อิสระ) | คัดลอกค่าโดยสมบูรณ์ |
| Reference Types (string, class, array) | คัดลอก reference (ชี้ object เดียวกัน) | สร้าง object ใหม่ คัดลอกค่าทุก field |
| String (immutable) | ปลอดภัย (ไม่สามารถแก้ไขได้) | ไม่จำเป็น |
สรุป
- Clone() สร้าง Stack ใหม่ที่มีข้อมูลเหมือนกันในขณะนั้น
- การเปลี่ยนแปลง Stack ต้นฉบับหลัง Clone (Push/Pop/Clear) ไม่ส่งผล ต่อ Stack ที่ Clone ไป
- สำหรับ string และ value types: Clone() ทำงานเหมือน Deep Copy เพราะ immutable
- สำหรับ custom class objects: ต้องระวัง shared reference
- Generic Stack<T> มี Copy Constructor:
new Stack<T>(existingStack)ใช้งานสะดวกกว่า