13.10. Arrays of Generic Types

The J# language allows you to create arrays of generic types. As stated earlier in this chapter, a constructed generic type inherits the same set of qualities and capabilities that are associated with any other type you might use. Given this reality, you can imagine how an array of generic types might get declared. The following provides an example of this concept in action:

[J# code]
imports System.Collections.Generic.*;

public void DeclareGenericArrays() {
    List<int>[] intListArray = new List<int>[2];
    intListArray[0] = new List<int>();
intListArray[1] = new List<int>();

    List<int> tmpIntList = intListArray[1];
    tmpIntList.Add(123);

    List<int> intList = intListArray[1];
    int intVal = intList.get_Item(0);
}

There's no magic here. As you can see, the syntax conforms to the same model you see with non-generic types. Still, since arrays are such a vital structure, I thought it might be helpful to see how they are used with generics.

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.