object 儲存,值型別須裝箱/拆箱,效能損耗大。public class Stack<ItemType>
{
private ItemType[] items;
public void Push(ItemType data) { ... }
public ItemType Pop() { ... }
}
Stack<int> s = new Stack<int>();
s.Push(3);
int x = s.Pop(); // 無需轉型,無裝箱
Stack<Customer> s2 = new Stack<Customer>();
s2.Push(new Customer());
Customer c = s2.Pop(); // 型別安全
// s2.Push(3); // 編譯錯誤