Working with Collections and Lists


The .NET Framework contains thousands of types, a large proportion of which are data structures that are enumerable “ they support the ability for a contained or associated collection of items to be accessed in a sequential or key-based (random access) way.

To assist in working with enumerable types, the System.Collections namespace provides:

  • Collection interfaces that define standard methods and properties implemented by different types of data structures. These interfaces allow enumerable types to provide consistent functionality, and aid interoperability.

  • Functionality-rich implementations of many common collection classes such as lists and dictionaries. All these implement one or more of the common collection interfaces.

Collection Interfaces

Dealing with enumerable classes is a common task for developers, so the .NET Framework class library includes a set of interfaces in the System.Collections namespace that define contracts (of functionality) that enumerable classes implement. These interfaces provide consistency throughout the framework classes, making the life of a developer easier. Once you know how to work with one enumerable class that supports one or more common interfaces, you should, in theory, be able to work with any other enumerable class that supports the same interface in a uniform way, including the custom types that other developers create.

As developers, it's in your best interest to understand the collection interfaces in the System.Collections namespace. There aren't too many, and once you understand how they are organized, you can examine the interfaces that any enumerable type implements or returns from properties or methods, and determine what enumerable support a given type has. For example, any type that implements the IEnumerable interface supports forward-only iteration through its contained items. If a collection implements this interface, it also means that you can use the Visual Basic.NET and C# for each declaration with it.

Important

During compilation, compilers convert for...each declarations into calls to IEnumerable and its associated interface IEnumerator .

It's worth mentioning early on that most of the collection classes have many members (methods, properties, and so on) that are not defined in standard collection interfaces. The collection interfaces exist to define a common usage pattern across many different collection classes. The implementation of a given data structure, such as a queue, has many unique characteristics that are not defined in a standard interface “ they are just members of a particular type.

We'll focus mainly on the members defined by common interfaces in this chapter; however, there are many more methods available on most of the types covered, all of which are documented in the .NET SDK. Let's start by examining the core collection interfaces and some of the classes that implement them.




Professional ASP. NET 1.1
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)
ISBN: 0470384611
EAN: 2147483647
Year: 2006
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net