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

CHAPTER 19 GENERICS

Extension Methods with Generic Classes

Extension methods are described in detail in Chapter 7 and work just as well with generic classes. They allow you to associate a static method in one class with a different generic class and to invoke the method as if it were an instance method on a constructed instance of the class.

As with nongeneric classes, an extension method for a generic class must satisfy the following constraints:

It must be declared static.

It must be the member of a static class.

It must contain as its first parameter type the keyword this, followed by the name of the generic class it extends.

The following code shows an example of an extension method called Print on a generic class called

Holder<T>:

static class ExtendHolder

{

public static void Print<T>(this Holder<T> h)

{

T[] vals = h.GetValues();

Console.WriteLine("{0},\t{1},\t{2}", vals[0], vals[1], vals[2]);

}

}

class Holder<T>

{

T[] Vals = new T[3];

public Holder(T v0, T v1, T v2)

{ Vals[0] = v0; Vals[1] = v1; Vals[2] = v2; }

public T[] GetValues() { return Vals; }

}

class Program

{

 

static void Main(string[] args) {

 

 

var intHolder

= new Holder<int>(3, 5, 7);

 

 

var stringHolder = new Holder<string>("a1", "b2", "c3");

 

 

intHolder.Print();

 

}

stringHolder.Print();

 

 

 

 

}

 

 

 

This code produces the following output:

 

 

 

 

3,

5,

7

 

a1,

b2,

c3

 

 

 

 

 

486

CHAPTER 19 GENERICS

Generic Structs

Like generic classes, generic structs can have type parameters and constraints. The rules and conditions for generic structs are the same as those for generic classes.

For example, the following code declares a generic struct called PieceOfData, which stores and retrieves a piece of data, the type of which is determined when the type is constructed. Main creates objects of two constructed types—one using int and the other using string.

struct PieceOfData<T>

 

 

 

// Generic struct

{

 

 

 

 

 

 

 

 

public

PieceOfData(T value) { _data = value; }

private

T _data;

 

 

 

 

 

 

public

T Data

 

 

 

 

 

 

{

get

{ return _data; }

 

 

 

 

 

 

 

 

 

}

set

{ _data = value; }

 

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

class Program

 

 

 

 

 

 

{

 

 

 

Constructed type

static

void Main()

 

{

 

 

 

 

 

 

var

intData

= new PieceOfData<int>(10);

 

var

stringData = new PieceOfData<string>("Hi there.");

 

 

 

 

 

 

 

 

 

 

 

Constructed type

 

Console.WriteLine("intData

= {0}", intData.Data);

}

Console.WriteLine("stringData = {0}", stringData.Data);

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

This code produces the following output:

 

 

 

 

 

 

 

 

 

intData

=

10

 

 

 

 

 

 

stringData =

Hi there.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

487

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