Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

stack.Push(new Employee() { ID = "456", Name = "Margaret" });

Employee e1 = new Employee() { ID = "123", Name = "John" };

if (stack.Find(e1))

Console.WriteLine("Employee found.");

Specifying Multiple Constraints

You can specify multiple constraints in a generic type. For example, if you want the MyStackclass to manipulate objects of type Employeeand also implement the IComparableinterface, you can declare the generic type as:

public class MyStack where T : Employee, IComparable {

//...

}

Here, you are constraining that the MyStackclass must use types derived from Employeeand they must also implement the IComparableinterface.

The base class constraint must always be specified first, before specifying the interface.

Assuming that you have the following Managerclass deriving from the Employeeclass:

public class Manager : Employee, IComparable {

public int CompareTo(Manager obj) {

return base.CompareTo(obj);

}

}

The following statement is now valid:

MyStack stackM = new MyStack(3);

Multiple Type Parameter

So far you have seen only one type parameter used in a generic type, but you can have multiple type parameters. For example, the following MyDictionaryclass uses two generic type parameters — Kand V:

public class MyDictionary {

//...

}

To apply constraints on multiple type parameters, use the wherekeyword multiple times:

public class MyDictionary

where K : IComparable where V : ICloneable {

//...

}

Generic Interfaces

Generics can also be applied on interfaces. The following example defines the IMyStackinterface:

interface IMyStack where T : IComparable {

void Push(T item);

T Pop();

bool Find(T keyword);

}

A class implementing a generic interface must supply the same type parameter as well as satisfy the constraints imposed by the interface.

The following shows the generic MyStackclass implementing the generic IMyStackinterface:

public class MyStack : IMyStack

where T : IComparable {

//...

}

Figure 9-4 shows the error reported by Visual Studio 2008 if the generic MyStackclass does not provide the constraint imposed by the generic interface.

Figure 94 Generic Structs Generics can also be applied to structs For - фото 141

Figure 9-4

Generic Structs

Generics can also be applied to structs. For example, suppose that you have a Coordinatestruct defined as follows:

public struct Coordinate {

public int x, y, z;

}

The coordinates for the Coordinatestruct takes in int values. You can use generics on the Coordinatestruct, like this:

public struct Coordinate {

public T x, y, z;

}

To use intvalues for the Coordinatestruct, you can do so via the following statements:

Coordinate pt1;

pt1.x = 5;

pt1.y = 6;

pt1.z = 7;

To use floatvalues for the Coordinatestruct, utilize the following statements:

Coordinate pt2;

pt2.x = 2.0F;

pt2.y = 6.3F;

pt2.z = 2.9F;

Generic Methods

In addition to generic classes and interfaces, you can also define generic methods. Consider the following class definition and the method contained within it:

public class SomeClass {

public void DoSomething(T t) {}

}

Here, DoSomething()is a generic method. To use a generic method, you need to provide the type when calling it:

SomeClass sc = new SomeClass();

sc.DoSomething(3);

The C# compiler, however, is smart enough to deduce the type based on the argument passed into the method, so the following statement automatically infers Tto be of type String:

sc.DoSomething("This is a string"); //---T is String---

This feature is known as generic type inference .

You can also define a constraint for the generic type in a method, like this:

public class SomeClass {

public void DoSomething(T t) where T : IComparable {

}

}

If you need the generic type to be applicable to the entire class, define the type Tat the class level:

public class SomeClass where T : IComparable {

public void DoSomething(T t) { }

}

In this case, you specify the type during the instantiation of SomeClass:

SomeClass sc = new SomeClass();

sc.DoSomething(3);

You can also use generics on static methods, in addition to instance methods as just described. For example, the earlier DoSomething()method can be modified to become a static method:

public class SomeClass {

public static void DoSomething(T t)

where T : IComparable {}

}

To call this static generic method, you can either explicitly specify the type or use generic type inference:

SomeClass.DoSomething(3);

//---or---

SomeClass.DoSomething(3);

Generic Operators

Generics can also be applied to operators. Consider the generic MyStackclass discussed earlier in this chapter. Suppose that you want to be able to join two MyStackobjects together, like this:

MyStack stack1 = new MyStack(4);

stack1.Push("A");

stack1.Push("B");

MyStack stack2 = new MyStack(2);

stack2.Push("C");

stack2.Push("D");

stack1 += stack2;

In this case, you can overload the +operator, as highlighted in the following code:

public class MyStack where T : IComparable {

private T[] _elements;

private int _pointer;

public MyStack(int size) {

_elements = new T[size];

_pointer = 0;

}

public void Push(T item) {

if (_pointer < _elements.Length - 1) {

throw new Exception("Stack is full.");

}

_elements[_pointer] = item;

_pointer++;

}

public T Pop() {

_pointer--;

if (_pointer < 0) {

return default(T);

}

return _elements[_pointer];

}

public bool Find(T keyword) {

bool found = false;

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x