Home

C++/CLI Collections:
Creating a Custom Collection Class

 

Overview of .NET Collection Interfaces

 

Introduction

When you use some of the built-in collection classes, they may not suit some particular need you have or they may not really be the type of collection appropriate for your application. In fact, after checking the .NET Framework built-in collection classes, you may not find the particular functionality you are looking for. As we saw in the previous two lessons, an alternative is to create your own collection class, which you can do from scratch.

To make it possible or even easier for anybody to create a good functional collection, you can use a class or derive one from those that ship with the .NET Framework. Collections are highly used by various other classes and Windows controls available in Microsoft Visual Studio. To provide a common functionality to the collections, sets, and series used in Windows applications and databases, the .NET Framework provides various interfaces that you can implement. Doing this gives you the double advantage of creating a collection class and getting familiar with the common collections you will use in graphical applications.

Choosing a Class or an Interface

When you want to use a collection in your application, you may first check what classes are available in the .NET Framework. If you don't find a suitable class, you can create your own that implements one or more interfaces. As it happens, the .NET Framework ships with many of them and your next step is to choose which one you prefer. Some of the most commonly used interfaces are

  • IComparer: If you derive a class from this interface, you can define how two objects would be compared for similarity or difference
  • IDictionary: This interface is used to create a collection class where each item is made of a  key=value combination
 

Home Copyright © 2007-2013, FunctionX Next