Chapter 9. Collections

In some situations it is necessary to hold more than a single item in your data collection. You might want to hold a group or collection of data that is related in some fashion in a larger construct. The C# language and the .NET Framework provide you with a number of opportunities for performing this type of value sorting in your code.

This chapter shows you how to work with groups of objects. It takes a close look at array lists, dictionaries, and collections as well as how to use them properly in your C# code for the best possible performance.

Examining Groups of Objects

This section looks at how the .NET base classes support data structures that consist of a group of similar objects. Chapter 3, "Objects and Types," introduced the ordinary array, the simplest data structure of this kind. The ordinary array is an instance of the class System.Array namespace, but C# wraps its own syntax around this class. System.Array has two advantages: it is relatively efficient for accessing an individual element given its index, and it has its own C# syntax, which obviously makes using it more intuitive. However, it also has a huge disadvantage: you must specify its size when you instantiate it. There is no facility for adding, inserting, or removing elements later on. You also have to have a numeric index in order to be able to access an element. This is not particularly useful if, for example, you are dealing with a set of employee records and need to look up a given record ...

Get Professional C# 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.