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

CHAPTER 19 GENERICS

Creating Variables and Instances

A constructed class type is used just like a regular type in creating references and instances. For example, the following code shows the creation of two class objects.

The first line shows the creation of an object from a regular, nongeneric class. This is a form that you should be completely familiar with by now.

The second line of code shows the creation of an object from generic class SomeClass, instantiated with types short and int. The form is exactly analogous to the line above it, with the constructed class forms in place of a regular class name.

The third line is the same semantically as the second line, but rather than listing the constructed type on both sides of the equals sign, it uses the var keyword to make the compiler use type inference.

 

MyNonGenClass

myNGC = new MyNonGenClass

();

 

Constructed class

 

 

 

Constructed class

 

 

 

 

 

 

 

 

 

 

SomeClass<short, int>

mySc1

=

new

SomeClass<short

int>();

 

var

mySc2

=

new

SomeClass<short, int>();

As with nongeneric classes, the reference and the instance can be created separately, as shown in Figure 19-6. The figure also shows that what is going on in memory is the same as for a nongeneric class.

The first line below the generic class declaration allocates a reference in the stack for variable myInst. Its value is null.

The second line allocates an instance in the heap and assigns its reference to the variable.

Figure 19-6. Using a constructed type to create a reference and an instance

Many different class types can be constructed from the same generic class. Each one is a separate class type, just as if it had its own separate nongeneric class declaration.

473

CHAPTER 19 GENERICS

For example, the following code shows the creation of two types from generic class SomeClass. The code is illustrated in Figure 19-7.

One type is constructed with types short and int.

The other is constructed with types int and long.

class SomeClass< T1, T2 >

 

// Generic class

{

 

 

 

...

 

 

 

}

 

 

 

class Program

 

 

 

{

 

 

 

static void Main()

 

 

{

 

 

 

var first =

new SomeClass<short,

int >();

// Constructed type

var second =

new SomeClass<int,

long>();

// Constructed type

...

Figure 19-7. Two constructed classes created from a generic class

474

CHAPTER 19 GENERICS

The Stack Example Using Generics

The following code shows the stack example implemented using generics. Method Main defines two variables: stackInt and stackString. The two constructed types are created using int and string as the type arguments.

class MyStack<T>

{

T[] StackArray;

int StackPointer = 0;

public void Push(T x)

{

if ( !IsStackFull ) StackArray[StackPointer++] = x;

}

public T Pop()

{

return ( !IsStackEmpty )

? StackArray[--StackPointer] : StackArray[0];

}

const int MaxStack = 10;

bool IsStackFull { get{ return StackPointer >= MaxStack; } } bool IsStackEmpty { get{ return StackPointer <= 0; } }

public MyStack()

{

StackArray = new T[MaxStack];

}

public void Print()

{

for (int i = StackPointer -1; i >= 0 ; i--) Console.WriteLine(" Value: {0}", StackArray[i]);

}

}

475

CHAPTER 19 GENERICS

class Program

{

static

void Main()

 

{

 

 

var

stackInt = new MyStack<int>();

var

stackString

= new MyStack<string>();

stackInt.Push(3);

stackInt.Push(5);

stackInt.Push(7);

stackInt.Print();

stackString.Push("Generics are great!"); stackString.Push("Hi there!");

stackString.Print();

}

}

This code produces the following output:

Value: 7

Value: 5

Value: 3

Value: Hi there!

Value: Generics are great!

476

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