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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

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

Интервал:

Закладка:

Сделать

public class MyStack {

private int[] _elements;

private int _pointer;

public MyStack(int size) {

_elements = new int[size];

_pointer = 0;

}

public void Push(int item) {

if (_pointer > _elements.Length - 1) {

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

}

_elements[_pointer] = item;

_pointer++;

}

public int Pop() {

_pointer--;

if (_pointer < 0) {

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

}

return _elements[_pointer];

}

}

In this case, the MyStackclass allows data of int type to be pushed into and popped out of the stack. The following statements show how to use the MyStackclass:

MyStack stack = new MyStack(3);

stack.Push(1);

stack.Push(2);

stack.Push(3);

Console.WriteLine(stack.Pop()); //---3---

Console.WriteLine(stack.Pop()); //---2---

Console.WriteLine(stack.Pop()); //---1---

As you can see, this stack implementation accepts stack items of the intdata type. To use this implementation for another data type, say String, you need to create another class that uses the stringtype. Obviously, this is not a very efficient way of writing your class definitions because you now have several versions of essentially the same class to maintain.

A common way of solving this problem is to use the Objectdata type so that the compiler will use late-binding during runtime:

public class MyStack {

private object[] _elements;

private int _pointer;

public MyStack(int size) {

_elements = new object[size];

_pointer = 0;

}

public void Push(object item) {

if (_pointer > _elements.Length - 1) {

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

}

_elements[_pointer] = item;

_pointer++;

}

public object Pop() {

_pointer-- ;

if (_pointer < 0) {

throw new Exception("Stack is empty.";

}

return _elements[_pointer];

}

}

One problem with this approach is that when you use the stack class, you may inadvertently pop out the wrong data type, as shown in the following highlighted code:

MyStack stack = new MyStack(3);

stack.Push(1);

stack.Push(2);

stack.Push("A");

//---invalid cast---

int num = (int)stack.Pop();

Because the Pop()method returns a variable of Objecttype, IntelliSense cannot detect during design time if this code is correct. It is only during runtime that when you try to pop out a stringtype and try to typecast it into an int type that an error occurs. Besides, type casting (boxing and unboxing) during runtime incurs a performance penalty.

To resolve this inflexibility, you can make use of generics.

Generic Classes

Using generics, you do not need to fix the data type of the items used by your stack class. Instead, you use a generic type parameter ( ) that identifies the data type parameter on a class, structure, interface, delegate, or procedure. Here's a rewrite of the MyStackclass that shows the use of generics:

public class MyStack {

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) {

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

}

return _elements[_pointer];

}

}

As highlighted, you use the type Tas a placeholder for the eventual data type that you want to use for the class. In other words, during the design stage of this class, you do not specify the actual data type that the MyStackclass will deal with. The MyStackclass is now known as a generic type.

When declaring the private member array _element, you use the generic parameter Tinstead of a specific type such as intor string:

private T[] _elements;

In short, you replace all specific data types with the generic parameter T.

You can use any variable name you want to represent the generic parameter. Tis chosen as the generic parameter for illustration purposes.

If you want the MyStackclass to manipulate items of type int, specify that during the instantiation stage ( intis called the type argument):

MyStack stack = new MyStack(3);

The stack object is now known as a constructed type, and you can use the MyStackclass normally:

stack.Push(1);

stack.Push(2);

stack.Push(3);

A constructed type is a generic type with at least one type argument.

In Figure 9-1 IntelliSense shows that the Push()method now accepts arguments of type int.

Figure 91 Trying to push a string value into the stack like this - фото 138

Figure 9-1

Trying to push a string value into the stack like this:

stack.Push("A"); //---Error---

generates a compile-time error. That's because the compiler checks the data type used by the MyStackclass during compile time. This is one of the key advantages of using generics in C#.

To use the MyStackclass for Stringdata types, you simply do this:

MyStack stack = new MyStack(3);

stack.Push("A");

stack.Push("B");

stack.Push("C");

Figure 9-2 summarizes the terms used in a generic type.

Figure 92 Using the default Keyword in Generics In the preceding - фото 139

Figure 9-2

Using the default Keyword in Generics

In the preceding implementation of the generic MyStackclass, the Pop()method throws an exception whenever you call it when the stack is empty:

public T Pop() {

_pointer--;

if (_pointer < 0) {

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

}

return _elements[_pointer];

}

Rather than throwing an exception, you might want to return the default value of the type used in the class. If the stack is dealing with intvalues, it should return 0; if the stack is dealing with string, it should return an empty string. In this case, you can use the default keyword to return the default value of a type:

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

Интервал:

Закладка:

Сделать

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

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


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

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

x