Chapter 9. Collections

Collections are one of the cornerstones of a modern programming language. Java has not always had good collection support, and only since Java 1.2 has the platform benefited from a strong collection class library. The collections provided in Microsoft .NET are less flexible than those in Java but include some interesting features that help to address the differences.

The first part of this chapter covers indexers, which are a useful language construct employed by all of the collection classes. We move on to explain the interfaces that define the basic collection contracts; then we discuss the collections themselves. Much of the chapter is given over to the individual collection classes. The most commonly used collections from Java are available and remain largely unchanged. Some new collections aren’t available in Java, and we explain their purpose and use. However, the .NET collection framework also has significant omissions, which Java developers will miss. We finish this chapter with a section on thread-safe collections and the classes that are available to build strongly typed custom collection types.

Indexers

Indexers are a new C# feature that allows classes to be indexed in a similar way to arrays. Indexers are particularly applicable for use in conjunction with collection data members. Using the array-style syntax provides a logical mechanism through which safe access can be provided to the underlying collection data. The .NET standard collection classes use indexers extensively, as we will see later in this chapter.

More Info

A comprehensive discussion of the declaration and usage of indexers can be found in Chapter 5.

Of particular use is the ability to overload an indexer, providing access based on different key types. The following example demonstrates the implementation of an indexer to provide access to a collection using both string and int as keys. This example uses an array as the underlying collection, but the same approach is equally applicable to any collection. The example we use is a set of paintbrushes. A brush can be selected by its position or its color. For the purpose of the example, only three brushes are manually defined.

public struct Brush {
    public readonly string Name;
    public readonly byte RedVal, BlueVal, GreenVal;

    public Brush(string name, byte red, byte green, byte blue) {
        Name = name;
        RedVal = red;
        BlueVal = blue;
        GreenVal = green;
    }
}

public class BrushPot {
    private Brush[] Brushes;

    public BrushPot() {
        Brushes = new Brush[3];
        Brushes[0] = new Brush("black",0,0,0);
        Brushes[1] = new Brush("red",255,0,0);
        Brushes[2] = new Brush("blue",0,0,255);
    }

    public Brush this[int key] {
        get {
            return Brushes[key];
        }
        set {
            Brushes[key] = value;
        }
    }
    public Brush this[string key] {
        get{
            switch (key) {
                case "black":
                    return Brushes[0];
                case "red":
                    return Brushes[1];
                case "blue":
                    return Brushes[2];
            }
            return Brushes[0];
        }
        set {
            switch (key) {
                case "black":
                    Brushes[0] = value;
                    break;
                case "red":
                    Brushes[1] = value;
                    break;
                case "blue":
                    Brushes[2] = value;
                    break;
            }
        }
    }
}

The indexers (indicated by boldface) allow a Brush to be retrieved using either its position in the array or its color. In the case of the color, we use a switch statement because we know the set of brush names. We could have used a foreach loop to look through the array and select the brush with the correct name if the number of brushes had been greater.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset