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

CHAPTER 19 GENERICS

What Are Generics?

With the language constructs you’ve learned so far, you can build powerful objects of many different types. You do this mostly by declaring classes that encapsulate the behavior you want and then creating instances of those classes.

All the types used in the class declarations so far have been specific types—either programmerdefined or supplied by the language or the BCL. There are times, however, when a class would be more useful if you could “distill” or “refactor” out its actions and apply them not just to the data types for which they are coded but for other types as well.

Generics allow you to do just that. You can refactor your code and add an additional layer of abstraction so that, for certain kinds of code, the data types are not hard-coded. This is particularly designed for cases in which there are multiple sections of code performing the same instructions, but on different data types.

That might sound pretty abstract, so we’ll start with an example that should make things clearer.

A Stack Example

Suppose first that you have created the following code, which declares a class called MyIntStack, which implements a stack of ints. It allows you to push ints onto the stack and pop them off. This, by the way, isn’t the system stack.

class MyIntStack

 

// Stack for ints

{

 

 

 

int

StackPointer = 0;

 

int[] StackArray;

 

// Array of int

 

int

 

int

 

 

public void Push( int x )

// Input type: int

{

...

 

 

 

 

 

}

int

 

 

 

 

 

public int Pop()

 

// Return type: int

{

...

 

 

}

 

 

 

 

 

...

}

466

CHAPTER 19 GENERICS

Suppose now that you would like the same functionality for values of type float. There are several ways you could achieve this. One way is to perform the following steps to produce the subsequent code:

Cut and paste the code for class MyIntStack.

Change the class name to MyFloatStack.

Change the appropriate int declarations to float declarations throughout the class declaration.

class MyFloatStack

// Stack for floats

{

 

 

int

StackPointer = 0;

 

float [] StackArray;

// Array of float

float

 

float

 

public void Push( float x )

// Input type: float

{

 

 

...

 

 

}

float

 

 

 

 

 

public float Pop()

// Return type: float

{

 

 

...

}

...

}

This method certainly works, but it’s error-prone, and has the following drawbacks:

You need to inspect every part of the class carefully to determine which type declarations need to be changed and which should be left alone.

You need to repeat the process for each new type of stack class you need (long, double, string, and so on).

After the process, you end up with multiple copies of nearly identical code, taking up additional space.

Debugging and maintaining the parallel implementations is inelegant and error-prone.

467

7

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