เมธอด Stack<T>.CopyTo() ใน C# ใช้คัดลอกสมาชิกใน Stack ไปยังอาร์เรย์หนึ่งมิติที่มีอยู่แล้ว เริ่มจากดัชนีที่ระบุ ลำดับการคัดลอกจะเป็นจาก top ของ Stack (LIFO order)
รูปแบบไวยากรณ์ (Syntax)
public virtual void CopyTo(T[] array, int arrayIndex);
array: อาร์เรย์ปลายทาง (ต้องมีขนาดพอ)arrayIndex: ดัชนีเริ่มต้นในอาร์เรย์ปลายทาง (0-based)
ตัวอย่างที่ 1: Stack ของ int พร้อม Clone และ CopyTo
using System;
using System.Collections.Generic;
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);
Console.WriteLine("Contains 750? = " + stack.Contains(750));
stack.Push(3000);
Console.WriteLine("\nStack elements...updated");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count (updated) = " + stack.Count);
Console.WriteLine("Contains 5000? = " + stack.Contains(5000));
// Clone แล้ว CopyTo array
Stack stack2 = new Stack(stack); // Copy constructor
Console.WriteLine("\nStack elements...cloned");
foreach (int val in stack2)
{
Console.WriteLine(val);
}
Console.WriteLine("Count of cloned stack = " + stack2.Count);
Console.WriteLine("\nCopying cloned stack to int array...");
int[] intArr = new int[stack2.Count];
stack2.CopyTo(intArr, 0);
foreach (int j in intArr)
{
Console.WriteLine(j);
}
}
}
ผลลัพธ์
Stack elements... (top to bottom) 2500 2000 1500 1250 1000 750 500 300 150 Count = 9 Contains 750? = True Stack elements...updated 3000 2500 2000 1500 1250 1000 750 500 300 150 Count (updated) = 10 Contains 5000? = False Stack elements...cloned 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of cloned stack = 10 Copying cloned stack to int array... 3000 2500 2000 1500 1250 1000 750 500 300 150
ตัวอย่างที่ 2: Stack ของ string
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
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);
Console.WriteLine("Contains 'XPS'? = " + stack.Contains("XPS"));
stack.Push("Ultrabook");
Console.WriteLine("\nStack elements...updated");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count (updated) = " + stack.Count);
Console.WriteLine("Contains 'Ultrabook'? = " + stack.Contains("Ultrabook"));
// Clone และ CopyTo string array
Stack stack2 = new Stack(stack);
Console.WriteLine("\nStack elements...cloned");
foreach (string val in stack2)
{
Console.WriteLine(val);
}
Console.WriteLine("Count = " + stack2.Count);
Console.WriteLine("\nCopying cloned stack to string array...");
string[] strArr = new string[stack2.Count];
stack2.CopyTo(strArr, 0);
foreach (string j in strArr)
{
Console.WriteLine(j);
}
}
}
ผลลัพธ์
Stack elements... (top to bottom) Notebook Laptop XPS Monitors Projectors Count = 5 Contains 'XPS'? = True Stack elements...updated Ultrabook Notebook Laptop XPS Monitors Projectors Count (updated) = 6 Contains 'Ultrabook'? = True Stack elements...cloned Ultrabook Notebook Laptop XPS Monitors Projectors Count = 6 Copying cloned stack to string array... Ultrabook Notebook Laptop XPS Monitors Projectors
สรุป
- ลำดับใน Array = Top-to-bottom ของ Stack (LIFO order)
- ต้องสร้าง Array ขนาดพอดี (หรือใหญ่กว่า) ก่อนเรียก CopyTo
- Non-generic
Stack.CopyTo(Array, int)สำหรับ Stack แบบเก่า - ใช้เมื่อต้องการ: random access, LINQ, serialization, snapshot
- Stack มี Copy Constructor
new Stack<T>(existingStack)ที่สะดวกกว่า