表示对象的先进先出集合。
public
static
void
QueueTest()
... {
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach (string number in numbers)
...{
Console.WriteLine(number);
}
Console.WriteLine(" Dequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
Console.WriteLine(" Contents of the first copy:");
foreach (string number in queueCopy)
...{
Console.WriteLine(number);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> queueCopy2 = new Queue<string>(array2);
Console.WriteLine(" Contents of the second copy, with duplicates and nulls:");
foreach (string number in queueCopy2)
...{
Console.WriteLine(number);
}
Console.WriteLine(" queueCopy.Contains("four") = {0}",
queueCopy.Contains("four"));
Console.WriteLine(" queueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine(" queueCopy.Count = {0}", queueCopy.Count);
}
... {
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach (string number in numbers)
...{
Console.WriteLine(number);
}
Console.WriteLine(" Dequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
Console.WriteLine(" Contents of the first copy:");
foreach (string number in queueCopy)
...{
Console.WriteLine(number);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> queueCopy2 = new Queue<string>(array2);
Console.WriteLine(" Contents of the second copy, with duplicates and nulls:");
foreach (string number in queueCopy2)
...{
Console.WriteLine(number);
}
Console.WriteLine(" queueCopy.Contains("four") = {0}",
queueCopy.Contains("four"));
Console.WriteLine(" queueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine(" queueCopy.Count = {0}", queueCopy.Count);
}
表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。
public
static
void
StackTest()
... {
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
// A stack can be enumerated without disturbing its contents.
foreach (string number in numbers)
...{
Console.WriteLine(number);
}
Console.WriteLine(" Popping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next item to destack: {0}",
numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop());
// Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Stack<string> stack2 = new Stack<string>(numbers.ToArray());
Console.WriteLine(" Contents of the first copy:");
foreach (string number in stack2)
...{
Console.WriteLine(number);
}
// Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
Stack<string> stack3 = new Stack<string>(array2);
Console.WriteLine(" Contents of the second copy, with duplicates and nulls:");
foreach (string number in stack3)
...{
Console.WriteLine(number);
}
Console.WriteLine(" stack2.Contains("four") = {0}",
stack2.Contains("four"));
Console.WriteLine(" stack2.Clear()");
stack2.Clear();
Console.WriteLine(" stack2.Count = {0}", stack2.Count);
}
... {
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
// A stack can be enumerated without disturbing its contents.
foreach (string number in numbers)
...{
Console.WriteLine(number);
}
Console.WriteLine(" Popping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next item to destack: {0}",
numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop());
// Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Stack<string> stack2 = new Stack<string>(numbers.ToArray());
Console.WriteLine(" Contents of the first copy:");
foreach (string number in stack2)
...{
Console.WriteLine(number);
}
// Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
Stack<string> stack3 = new Stack<string>(array2);
Console.WriteLine(" Contents of the second copy, with duplicates and nulls:");
foreach (string number in stack3)
...{
Console.WriteLine(number);
}
Console.WriteLine(" stack2.Contains("four") = {0}",
stack2.Contains("four"));
Console.WriteLine(" stack2.Clear()");
stack2.Clear();
Console.WriteLine(" stack2.Count = {0}", stack2.Count);
}
何时使用泛型集合:
通常情况下,建议您使用泛型集合,因为这样可以获得类型安全的直接优点而不需要从基集合类型派生并实现类型特定的成员。此外,如果集合元素为值类型,泛型集合类型的性能通常优于对应的非泛型集合类型(并优于从非泛型基集合类型派生的类型),因为使用泛型时不必对元素进行装箱。