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 Rectangle : Shape {

public override sealed double Area() {

return this.length * this.width;

}

}

The only way to add a new method Diagonal()to the Rectangleclass is to create a new class that derives from it, like this:

public class NewRectangle : Rectangle {

public double Diagonal() {

return Math.Sqrt(Math.Pow(this.length, 2) + Math.Pow(this.width, 2));

}

}

In C# 3.0, you just use the new extension method feature to add a new method to an existing type. To add the Diagonal()method to the existing Rectangleclass, define a new static class and define the extension method (a static method) within it, like this:

public static class MethodsExtensions {

public static double Diagonal(this Rectangle rect) {

return Math.Sqrt(Math.Pow(rect.length, 2) + Math.Pow(rect.width, 2));

}

}

In this example, Diagonal()is the extension method that is added to the Rectangleclass. You can use the Diagonal()method just like a method from the Rectangleclass:

Rectangle r = new Rectangle();

r.length = 4;

r.width = 5;

//---prints out: 6.40312423743285---

Console.WriteLine(r.Diagonal());

The first parameter of an extension method is prefixed by the thiskeyword, followed by the type it is extending (Rectangle in this example, indicating to the compiler that this extension method must be added to the Rectangleclass). The rest of the parameter list (if any) is then the signature of the extension method. For example, to pass additional parameters into the Diagonal()extension method, you can declare it as:

public static double Diagonal(this Rectangle rect, int x, int y) {

//---additional implementation here---

return Math.Sqrt(Math.Pow(rect.length, 2) + Math.Pow(rect.width, 2));

}

To call this modified extension method, simply pass in two arguments, like this:

Console.WriteLine(r.Diagonal(3,4));

Figure 6-4 shows IntelliSense providing a hint on the parameter list.

Figure 64 Although an extension method is a useful new feature in the C - фото 114

Figure 6-4

Although an extension method is a useful new feature in the C# language, use it sparingly. If an extension method has the same signature as another method in the class it is trying to extend, the method in the class will take precedence and the extension method will be ignored.

Access Modifiers

Chapter 4 discussed two primary access modifiers — publicand private, and introduced two others: protectedand internal. Let's take a look at how the latter are used. Consider the following class definition:

public class A {

private int v;

public int w;

protected int x;

internal int y;

protected internal int z;

}

The Aclass has four data members, each with a different access modifiers. The fifth data member, z, has a combination of two access modifiers — protectedand internal. To see the difference between all these different modifiers, create an instance of Aand observe the members displayed by IntelliSense.

Figure 6-5 shows that only the variables w, y, and zare accessible.

C 2008 Programmers Reference - изображение 115

Figure 6-5

At this moment, you can conclude that:

□ The privatekeyword indicates that the member is not visible outside the type (class).

□ The publickeyword indicates that the member is visible outside the type (class).

□ The protectedkeyword indicates that the member is not visible outside the type (class).

□ The internalkeyword indicates that the member is visible outside the type (class).

□ The protected internalkeyword combination indicates that the member is visible outside the type (class).

Now define a second class, B, that inherits from class A:

public class B : A {

public void Method() {}

}

Try to access the class Avariables from within Method(). In Figure 6-6, IntelliSense shows the variables that are accessible.

Figure 66 As you can see member xis now visible in addition to w y and z - фото 116

Figure 6-6

As you can see, member xis now visible (in addition to w, y, and z), so you can conclude that:

□ The privatekeyword indicates that the member is not visible outside the type (class) or to any derived classes.

□ The publickeyword indicates that the member is visible outside the type (class) and to all derived classes.

□ The protectedkeyword indicates that the member is not visible outside the type (class) but is visible to any derived classes.

□ The internalkeyword indicates that the member is visible outside the type (class) as well as to all derived classes.

□ The protected internalkeyword combination indicates that the item is visible outside the type (class) as well as to all derived classes.

From these conclusions, the difference among private, public, and protectedis obvious. However, there is no conclusive difference between internaland protected internal. The internalaccess modifier indicates that the member is only visible within its containing assembly. The protected internalkeyword combination indicates that the member is visible to any code within its containing assembly as well as derived types.

Besides applying the access modifiers to data members, you can also use them on type definitions. However, you can only use the privateand publicaccess modifiers on class definitions.

Inheritance and Constructors

Consider the following BaseClassdefinition consisting of one default constructor:

public class BaseClass {

//---default constructor---

public BaseClass() {

Console.WriteLine("Constructor in BaseClass");

}

}

Anther class, DerivedClassinheriting from the BaseClass, also has a default constructor:

public class DerivedClass : BaseClass {

//---default constructor---

public DerivedClass() {

Console.WriteLine("Constructor in DerivedClass");

}

}

So when an object of DerivedClassis instantiated, which constructor will be invoked first? The following statement shows that the constructor in the base class will be invoked before the constructor in the current class will be invoked:

DerivedClass dc = new DerivedClass();

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

Интервал:

Закладка:

Сделать

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

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


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

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

x