
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Replacing the Stack and Queue with Their Generic Counterparts
|
231
The GetRange method performs a shallow copy (similar to the Clone method of the
ArrayList) of a range of elements in the List object. In this case the range of ele-
ments includes all elements.
The ArrayList has a default initial capacity of 16 elements, while the
List<T> has a default initial capacity of only 4 elements. This means
that the
List<T> will have to be resized (and reallocated) 3 times by the
time the 17th element is added, whereas the
ArrayList will have to be
resized only one time. This should be taken into account when evalu-
ating the performance of your application.
See Also
See the “System.Collections.ArrayList Class” and “System.Collections.Generic.List
Class” topics in the MSDN documentation.
4.5 Replacing the Stack and Queue with Their
Generic Counterparts
Problem
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 ver-
sions. This is imperative when you find that structures or other value types are being
stored in these data structures, resulting in boxing/unboxing operations.
Solution
Replace all occurrences of the System.Collections.Stack and System.Collection.
Queue
objects with the System.Collections.Generic.Stack ...