This is a mirror of official site: http://jasper-net.blogspot.com/

From Normal Collections to Generic Collections

| Thursday, September 8, 2011
Prior to .NET Version 2.0 if you wanted to hold a collection of string, integers, or objects you had to work with some of the classes in the System.Collections namespace.

This namespace contains classes and interfaces needed to define collections of objects. Some of the classes are listed in Table 1.
Class     Description
ArrayList     An ArrayList is similar to an array, but the ArrayList class allows you to add items without managing the size of the list yourself (much like a VB Collection object).
CollectionBase     This class is the base for a strongly-typed collection, critical for creating collection classes, described below.
DictionaryBase     This class is the base for a strongly-typed collection utilizing associated keys and values. This is similar to the Dictionary object found in VBScript and used by many ASP developers.
SortedList     This class represents a collection of keys and values, and is automatically sorted by the key. You can still access the values with either the key or the index number.

Table 1. System.Collections classes

Assume you create a class called Customer. The Customer class has a number of properties, methods, and events. You then want to create several Customer objects within one structure so you can iterate through the collection, performing some operation on each object. You could just create a variable of type Array, ArrayList, or SortedList, but these standard collections can hold any type of object. In other words, you could add a Customer object as the first item in the collection, but you could then add an Employee object as the second item, a string as the third, and so on. If you try to iterate over this collection and apply the Update method, it might work on the Customer and Employee objects, but when your loop attempts to call the Update method of a String object, it will certainly fail.
To get around this sticky situation, you can create your own class that looks like a generic collection object, but restricts its members to only one data type. This new class will include the standard methods of the collection base type, such as the Add and Remove methods, as well as an Item property, but your class’ Add method will only allow you to add Customer objects.

The .NET Framework 2.0 introduced a new namespace called System.Collections.Generics which provides a series of classes that closely resemble the classes in System.Collections. However, these new classes automatically provide the ability to contain only one type of object.

System.Collections Example

Assume that you need to create a Customer object. The structure of the Customer object is very simple, containing just a CompanyName property. Imagine that you want to create a collection class named Customers (note the “s” on the end) to hold a set of Customer objects. You first need to create the Customer class as follows:

public class Customer
{
  public string CustomerName;
}


Read more: Paul Sheriff's Blog for the Real World
QR: from-normal-collections-to-generic-collections.aspx

Posted via email from Jasper-net

0 comments: