Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 19 GENERICS

Generic Classes

Now that you’ve seen a generic class, let’s look at generic classes in more detail and see how they’re created and used.

As you know, there are two steps for creating and using your own regular, nongeneric classes: declaring the class and creating instances of the class. But generic classes are not actual classes but templates for classes—so you must first construct actual class types from them. You can then create references and instances from these constructed class types.

Figure 19-3 illustrates the process at a high level. If it’s not all completely clear yet, don’t worry— we’ll cover each part in the following sections.

1.Declare a class, using placeholders for some of the types.

2.Provide actual types to substitute in for the placeholders. This gives you an actual class definition, with all the “blanks” filled in.

3.Create instances from the “filled-in” class definition.

Figure 19-3. Creating instances from a generic type

470

CHAPTER 19 GENERICS

Declaring a Generic Class

Declaring a simple generic class is much like declaring a regular class, with the following differences:

Place a matching set of angle brackets after the class name.

Between the angle brackets, place a comma-separated list of the placeholder strings that represent the types, to be supplied on demand. These are called type parameters.

Use the type parameters throughout the body of the declaration of the generic class to represent the types that should be substituted in.

For example, the following code declares a generic class called SomeClass. The type parameters are listed between the angle brackets and then used throughout the body of the declaration as if they were real types.

Type parameters

class SomeClass < T1, T2 >

{Normally, types would be used in these positions.

 

 

 

 

 

public

T1

SomeVar =

new

T1();

 

public

T2

OtherVar =

new

T2();

}

 

 

 

Normally, types would be used in these positions.

There is no special keyword that flags a generic class declaration. Instead, the presence of the type parameter list, demarcated with angle brackets, distinguishes a generic class declaration from a regular class declaration.

471

CHAPTER 19 GENERICS

Creating a Constructed Type

You cannot create class objects directly from a generic class. First, you need to tell the compiler what actual types should be substituted for the placeholders (the type parameters). The compiler takes those actual types and creates a template from which it creates actual class objects.

To construct a class type from a generic class, list the class name and supply real types between the angle brackets, in place of the type parameters. The real types being substituted for the type parameters are called type arguments.

Type arguments

SomeClass< short, int >

The compiler takes the type arguments and substitutes them for their corresponding type parameters throughout the body of the generic class, producing the constructed type—from which actual class instances are created.

Figure 19-4 shows the declaration of generic class SomeClass on the left. On the right, it shows the constructed class created by using the type arguments short and int.

Figure 19-4. Supplying type arguments for all the type parameters of a generic class produces a constructed

class from which actual class objects can be created.

Figure 19-5 illustrates the difference between type parameters and type arguments.

Generic class declarations have type parameters, which act as placeholders for types.

Type arguments are the actual types you supply when creating a constructed type.

Figure 19-5. Type parameters versus type arguments

472

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]