Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

354

Part III Creating Components

Chapter 18 Quick Reference

To

Do this

Instantiate an object by using a generic type

Create a new generic type

Specify the appropriate generic type parameter. For example:

Queue<int> myQueue = new Queue<int>();

Define the class using a type parameter. For example:

public class Tree<TItem>

{

...

}

Restrict the type that can be substituted for the generic type parameter

Specify a constraint by using a where clause when defining the class. For example:

public class Tree<TItem>

where TItem : IComparable<TItem>

{

...

}

Define a generic method

Define the method by using type parameters. For example:

 

static void InsertIntoTree<TItem>

 

(Tree<TItem> tree, params TItem[] data)

 

{

 

...

 

}

Invoke a generic method

Provide types for each of the type parameters. For example:

 

InsertIntoTree<char>(charTree, ‘Z’, ‘X’);

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