We spent time explaining what generic collections are and how they work so that you’d have an appreciation for how they’re created and what they can do. Most of the time, you won’t need to create your own collection, because the .NET Framework provides four very useful generic collections, as we discussed earlier (
List, Stack, Queue, and
Dictionary). We describe each in turn in the next few sections.
The classic problem with the
Array type is its fixed size. If you do not know in advance how many objects an array will hold, you run the risk of declaring either too small an array (and running out of room) or too large an array (and wasting memory).
List class is, essentially, an array whose size is dynamically increased as required.
Lists provide a number of useful methods and properties for their manipulation. Some of the most important are shown in Table 14-2.
Table 14-2. List properties and methods
Method or property
Property to get or set the number of elements the
Property to get the number of elements currently in the list
Property that .NET requires for the
Public method to add an object ...