Introduction


Collections are groups of items; in .NET, collections contain objects (including boxed value types). Each object contained in a collection is called an element. Some collections contain a straightforward list of elements, while others (dictionaries) contain a list of key and value pairs. The following collection types consist of a straightforward list of elements:

 System.Collections.ArrayList System.Collections.BitArray System.Collections.Queue System.Collections.Stack System.Collections.Generic.LinkedList<T> System.Collections.Generic.List<T> System.Collections.Generic.Queue<T> System.Collections.Generic.Stack<T> 

The following collection types are dictionaries:

 System.Collections.Hashtable System.Collections.SortedList System.Collections.Generic.Dictionary<T,U> System.Collections.Generic.SortedList<T,U> 

These collection classes are organized under the System.Collections and the System.Collections.Generic namespaces. In addition to these namespaces, another namespace called System.Collections.Specialized contains a few more useful collection classes. These classes might not be as well known as the previous classes, so here is a short explanation of the collection classes under the System.Collections.Specialized namespace:


ListDictionary

This class operates very similarly to the Hashtable. However, this class beats out the Hashtable on performance when it contains 10 or fewer elements.


HybridDictionary

This class consists of two internal collections, the ListDictionary and the Hashtable. Only one of these classes is used at any one time. The ListDictionary is used while the collection contains 10 or fewer elements, and then a switch is made to use a Hashtable when the collection grows beyond 10 elements. This switch is made transparently to the developer. Once the Hashtable is used, this collection cannot revert to using the ListDictionary even if the elements number 10 or fewer. Also note that, when using strings as the key, this class supports both case-sensitive (with respect to the invariant culture)and case-insensitive string searches through setting a Boolean value in the constructor.


CollectionsUtil

This class contains two static methods: one to create a case-insensitive Hashtable and another to create a case-insensitive SortedList. When you directly create a Hashtable and SortedList object, you always create a case-sensitive Hashtable or SortedList, unless you use one of the constructors that take an IComparer and pass CaseInsensitiveComparer.Default to it.


NameValueCollection

This collection consists of key and value pairs, which are both of type String. The interesting thing about this collection is that it can store multiple string values with a single key. The multiple string values are comma-delimited. The String.Split method is useful when breaking up multiple strings in a value.


StringCollection

This collection is a simple list containing string elements. This list accepts null elements as well as duplicate strings. This list is case-sensitive.


StringDictionary

This is a Hashtable that stores both the key and value as strings. Keys are converted to all-lowercase letters before being added to the Hashtable, allowing for case-insensitive comparisons. Keys cannot be null, but values may be set to null.

The C# compiler also supports a fixed-size array. Arrays of any type may be created using the following syntax:

 int[] foo = new int[2]; T[] bar = new T[2]; 

where foo is an integer array containing exactly two elements and bar is an array of unknown type T.

Arrays come in several styles as well: multidimensional, jagged, and even multidimensional jagged. Multidimensional arrays are defined as shown here:

 int[,] foo = new int[2,3];      // A 2-dimensional array                                 // containing 6 elements int[,,] bar = new int[2,3,4];   // A 3-dimensional array                                 // containing 24 elements 

A two-dimensional array is usually described as a table with rows and columns. The foo array would be described as a table of two rows, each containing three columns of elements. A three-dimensional array can be described as a cube with layers of tables. The bar array could be described as four layers of two rows, each containing three columns of elements.

Jagged arrays are arrays of arrays. If you picture a jagged array as a one-dimensional array with each element in that array containing another one-dimensional array, it could have a different number of elements in each row. A jagged array is defined as follows:

 int[][] baz = new int[2][] {new int[2], new int[3]}; 

The baz array consists of a one-dimensional array containing two elements. Each of these elements consists of another array, the first array having two elements and the second array having three.

The rest of this chapter contains recipes dealing with arrays and the various collection types.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

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