Nested types (what is??) )

Source: Internet
Author: User

Nested types have free access to members of the outer type, regardless of whether those members are private. Outer type if you want to access nested types, you are restricted by access rules.

Nested types default to private, regardless of whether the outer layer is a class or struct, but can be set to public, protected internal, protected, internal, or private.

Nested types default to private, regardless of whether the outer type is type or structure, but can be set to public, protected internal, protected, internal, or private.

If the nested type is private or protected, the type of the nested type other than the outer layer type is inaccessible, and another nested type in the external type can access it, but it cannot use its private or protected members. If a member in a nested type is set to public or internal, the outer type and other nested types declared by the same outer type can access those members of the nested type, but those members are still hidden outside the outer layer type.

If the nested type is public or internal, it will be similar to a normal type, and other types can use Morthtype.sontype as normal, but the nested type is still free to access all members of the outer type. In fact, this gives a type a strong accessibility, but also exposes a type of private members, which destroys the encapsulation of the type. In this case, the outer type functions as a namespace, which also gives the ability to type some namespaces.
The original example is as follows:

[Start citation]

A static constructor for a nested class does not initialize with the triggering of an external class. As a result, it is possible to avoid the initialization time of creation, and when an inline class is required, the nested class starts initializing before it begins to initialize.

public class Outside
{
Static Outside ()
{
Console.WriteLine ("Outside inilizlized");
}

public void Sayit ()
{
Nested.run ();
}

Private Class Nested
{
Static Nested ()
{
Console.WriteLine ("Nested initilized");
}

public static void Run ()
{
Console.WriteLine ("Nested Run");
}
}
}

Execution results

Outside o = new Outside ();//print "Outside inilizlized"
Console.ReadLine ();
O.sayit ();//print "Nested initilized" and print "Nested Run" first
Console.ReadLine ();

Nested types (what is??) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.