Chapter 8: Collections and Sorting

Chapter 8
Collections and Sorting
  • Comparing and Sorting
  • Storing Objects in Collection Classes

In this chapter, we’ll examine the classes and interfaces used for comparing, sorting, and storing objects in collections in the Microsoft .NET Framework. We’ll begin by looking at two interfaces: IComparable and IComparer. By implementing the IComparable interface with your types, you can provide them with a standard comparison mechanism that’s interoperable with classes in the .NET Framework. The IComparer interface is implemented by classes that provide comparison services to other types. These two interfaces are frequently used for comparison and sorting in the .NET Framework and enable you to easily leverage the built-in support for sorting provided by many other types in the .NET Framework, including the Array class.

This chapter also examines the collection classes included with the .NET Framework. Up to now, we’ve used only arrays as containers for other objects. This chapter explores the more advanced classes that can be used to manage collections of objects, such as the Queue, Stack, and Hashtable classes.

Later chapters, including the chapters on programming Microsoft Windows Forms, will use collection classes extensively. Understanding the similarities and differences between the collection classes will help you determine the type of collection best suited for a particular purpose.



Part III: Programming Windows Forms