logo search
CSharp_Prog_Guide

Вложенные типы

Тип, определенный внутри класса или структуры, называется вложенным типом. Пример.

----

Независимо от того, являются ли внешние типы классом или структурой, вложенные типы по умолчанию являются private, но могут быть также public, protected internal, protected, internal или private. В предыдущем примере тип Nested недоступен для внешних типов, но он может являться открытым:

class Container

{

public class Nested

{

Nested() { }

}

}

Вложенный или внутренний тип может получить доступ к содержащему или внешнему типу. Чтобы получить доступ к содержащему типу, передайте его в качестве конструктора во вложенный тип. Пример.

---

Вложенные типы могут получать доступ к закрытым и защищенным членам содержащего типа, включая любые наследуемые закрытые или защищенные члены.

В предыдущем объявлении полным именем класса Nested является Container.Nested. Это имя используется для создания нового экземпляра вложенного класса, как показано ниже:

Container.Nested nest = new Container.Nested();

Access Modifiers

Classes and structs can be restricted so that only the program or namespace they are declared in may use them. Class members can be restricted so that only derived classes can use them, or restricted so that only classes in the current namespace or program can use them. Access modifiers are keywords added to the class, struct, or member declaration to specify these restrictions. Those keywords are public, private, protected, and internal. For example:

public class Bicycle

{

public void Pedal() { }

}

Class and Struct Accessibility

Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types in the same assembly. By default, classes and structs are declared as internal unless the public keyword is added to the class definition, as in the previous example. Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself; it always has access to itself and all its own members.

Class and Struct Member Accessibility

Class or struct members can be declared with one of five types of access. They can be public or internal, just like the classes and structs themselves. When a class member is declared as protected by using the protected keyword, only derived types that use the class as a base can access the member. By combining the protected and internal keywords, a class member can be marked protected internal; only derived types or types in the same assembly can access that member.

Finally, a class or struct member can be declared as private with the private keyword, which indicates that only the class or struct declaring the member can access that member.

To set the access level for a class or struct member, add the appropriate keyword to the member declaration. Here are some examples: