December 2007
Intermediate to advanced
896 pages
19h 57m
English
You want to enhance the performance of your application as well as make the code easier to work with by replacing all Stack and Queue objects with their generic versions. This is imperative when you find that structures or other value types are being stored in these data structures, resulting in boxing/unboxing operations.
Replace all occurrences of the System.Collections.Stack and System.Collection.Queue objects with the System.Collections.Generic.Stack and System.Collection.Generic.Queue objects.
Here is a simple example of using a System.Collections.Queue object:
public static void UseNonGenericQueue()
{
// Create a non-generic Queue object.
Queue numericQueue = new Queue();
// Populate Queue (causing a boxing operation to occur).
numericQueue.Enqueue(1);
numericQueue.Enqueue(2);
numericQueue.Enqueue(3);
// De-populate Queue and display items (causing an unboxing operation to
occur)
Console.WriteLine(numericQueue.Dequeue());
Console.WriteLine(numericQueue.Dequeue());
Console.WriteLine(numericQueue.Dequeue().ToString());
}Here is that same code using a System.Collections.Generic.Queue object:
public static void UseGenericQueue() { // Create a generic Queue object. Queue<int> numericQueue = new Queue<int>(); // Populate Queue. numericQueue.Enqueue(1); numericQueue.Enqueue(2); numericQueue.Enqueue(3); // De-populate Queue and display items. Console.WriteLine(numericQueue.Dequeue()); Console.WriteLine(numericQueue.Dequeue()); ...Read now
Unlock full access