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

CHAPTER 7 CLASSES AND INHERITANCE

Class Access Modifiers

A class can be seen and accessed by other classes in the system. This section explains the accessibility of classes. Although I’ll use classes in the explanations and examples since that’s what we’ve covered so far in the text, the accessibility rules also apply to the other types I’ll cover later.

The term visible is sometimes used for the term accessible. They can be used interchangeably. There are two levels of class accessibility: public and internal.

A class marked public can be accessed by code from any assembly in the system. To make a class visible to other assemblies, use the public access modifier, as shown here:

Keyword

public class MyBaseClass

{...

A class marked internal can only be seen by classes within its own assembly.

This is the default accessibility level, so unless you explicitly specify the modifier public in the class declaration, code outside the assembly cannot access the class.

You can explicitly declare a class as internal by using the internal access modifier.

Keyword

internal class MyBaseClass

{ ...

Figure 7-13 illustrates the accessibility of internal and public classes from outside the assembly. Class MyClass is not visible to the classes in the assembly on the left, because it’s marked internal. Class OtherClass, however, is visible to the classes on the left, because it’s marked public.

Figure 7-13. Classes from other assemblies can access public classes but cannot access internal classes.

181

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