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

CHAPTER 7 CLASSES AND INHERITANCE

Member Access Modifiers

The previous two sections explained class accessibility. With class accessibility, there are only two modifiers—internal and public. This section covers member accessibility. Class accessibility describes the visibility of a class; member accessibility describes the visibility of the members of a class object.

Each member declared in a class is visible to various parts of the system, depending on the access modifier assigned to it in its class declaration. You’ve seen that private members are visible only to other members of the same class, while public members can be visible to classes outside the assembly as well. In this section, we’ll look again at the public and private access levels, as well as the three other levels of accessibility.

Before looking at the specifics of member accessibility, there are some general things we need to cover first:

All members explicitly declared in a class’s declaration are visible to each other, regardless of their accessibility specification.

Inherited members are not explicitly declared in a class’s declaration, so, as you’ll see, inherited members might or might not be visible to members of a derived class.

There are five member access levels:

public

private

protected

internal

protected internal

You must specify member access levels on a per-member basis. If you don’t specify an access level for a member, its implicit access level is private.

A member cannot be more accessible than its class. That is, if a class has an accessibility level limiting it to the assembly, individual members of the class cannot be seen outside the assembly, regardless of their access modifiers, even public.

184

CHAPTER 7 CLASSES AND INHERITANCE

Regions Accessing a Member

The member access modifiers in a class’s declaration specify which other types can and cannot access which members of the class. For example, the following declaration shows members declared with the five access levels.

public class MyClass

 

{

 

public

int Member1;

private

int Member2;

protected

int Member3;

internal

int Member4;

protected internal int Member5;

...

The access levels are based on two characteristics with regard to the class being declared:

Whether the class is derived from the class being declared

Whether a class is in the same assembly as the class being declared

These two characteristics yield four groups, as illustrated in Figure 7-15. In relation to the class being declared, another class can be any of the following:

In the same assembly and derived from it (bottom right)

In the same assembly but not derived from it (bottom left)

In a different assembly and derived from it (top right)

In a different assembly and not derived from it (top left)

These characteristics are used to define the five access levels.

Figure 7-15. Areas of accessibility

185

CHAPTER 7 CLASSES AND INHERITANCE

Public Member Accessibility

The public access level is the least restrictive. All classes both inside and outside the assembly have free access to the member. Figure 7-16 illustrates the accessibility of a public class member of MyClass.

To declare a member public, use the public access modifier, as shown.

Keyword

public int Member1;

Figure 7-16. A public member of a public class is visible to all classes in the same assembly or other

assemblies.

Private Member Accessibility

The private access level is the most restrictive.

A private class member can be accessed only by members of its own class. It cannot be accessed by other classes, including classes that are derived from it.

A private member can, however, be accessed by members of classes nested in its class. Nested classes are covered in Chapter 25.

Figure 7-17 illustrates the accessibility of a private member.

Figure 7-17. A private member of any class is visible only to members of its own class (or nested classes).

186

CHAPTER 7 CLASSES AND INHERITANCE

Protected Member Accessibility

The protected access level is like the private access level, except that it also allows classes derived from the class to access the member. Figure 7-18 illustrates protected accessibility. Notice that even classes outside the assembly that are derived from the class have access to the member.

Figure 7-18. A protected member of a public class is visible to members of its own class or classes derived

from it. The derived classes can even be in other assemblies.

Internal Member Accessibility

Members marked internal are visible to all the classes in the assembly but not to classes outside the assembly, as illustrated in Figure 7-19.

Figure 7-19. An internal member of a public class is visible to members of any class in the same assembly

but not to classes outside the assembly.

187

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