C # usage and features of Nested classes

Source: Internet
Author: User

The nested class is defined in the class. Classes with embedded classes are called external classes. According to the "deep understanding of Nested classes and internal classes", the nested classes are classified into static nesting classes and non-static nesting classes. The non-static nested class pages are called internal classes. Nested classes are another of composite in UML.
The nested class is defined in the class. Classes with embedded classes are called external classes. According to the "deep understanding of Nested classes and internal classes", the nested classes are classified into static nesting classes and non-static nesting classes. The non-static nested class pages are called internal classes. In UML, nested classes are another form of code representation of composite, indicating a higher Coupling Degree and a closer relationship with external classes.

The general class access modifier can be defined as the default internal or public, while the embedded class has many options, such as protected, internal, public, and default private.

Access restrictions for embedded and external classes
Nested classes can access methods, attributes, and fields of external classes regardless of access modifiers. For example:

Public Class
{
Private Static int _ aint;
Private int _ instanceint;
Private Static void amethod ()
{
Console. writeline (_ aint );
}
Public void sayit ()
{
Nesteda. Method (this );
}
/* Nested class definition */
Private class nesteda
{
Public static void method (a)
{
// Static member
_ Aint = 100;
Amethod ();
// Instance Member
A. _ instanceint = 10;
A. sayit ();
}
}
}
However, external classes can only access fields, methods, and attributes of the public and internal Nested classes. Example:

Public Class
{
Public static void amethod ()
{
// Succeeded
Nesteda. staticmethod ();
// Compilation Error
Nesteda. _ Int = 100;

Nesteda ins = new nesteda ();
// Succeeded
INS. Method ();
// Compilation Error
INS. _ instanceint = 100;
}
/* Nested class definition */
Private class nesteda
{
Private Static int _ int;
Private int _ instanceint;
Public static void staticmethod (){}
Public void method (){}
}
}
When a nested class accesses methods, fields, and attributes of an external class instance. Generally, external classes are input using constructors. As follows:

Public Class
{
Private int _;

/* Nested class definition */
Private class nesteda
{
Public nesteda ()
{
A. _ A = 9;
}
}
}
Inheritance
The inherited class, that is, the class that inherits the external class, can only use the public or internal (same program set) method of the nested class in the parent class. However, an inherited class can define an embedded class and inherit the nested class from the parent class. For example:

Public Class
{
/* Nested class definition */
Protected class nested
{
Protected virtual void basenested_method (){}
}
}

Public Class C:
{
/* Embedded class definition */
Protected class c_nested: Nested
{
Protected override void basenested_method ()
{
// Rewrite part
}
}
}
Because c inherits from a, c_nested can inherit from the nested class to obtain the opportunity to override the parent nested class. However, nested must be an inherited class and accessible (non-private, sealed, static ).

The nested class can be any data attribute of the external class at will, while the external class access the nested class can only follow the access modifier. From this perspective, nested classes are complementary to external classes. Nested classes can be used to obtain better encapsulation and increase the maintainability and readability of external classes.

From the perspective of program structure, nested classes are logically closer to the classes used. It can more effectively indicate the closeness between classes. Provides an alternative namespace for class management.

Lazy Loading

Static constructors of Nested classes are not initialized with the triggering of external classes. Therefore, the initialization time during creation can be effectively avoided. When embedded classes are required, the nested classes can be initialized before initialization starts.

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 result

Outside o = new outside (); // print "outside inilizlized"
Console. Readline ();
O. sayit (); // print "nested initilized" and then "nested run"
Console. Readline ();
This feature is generally found in some C # Singleton modes, which can be called the fully lazy Singleton mode. The following is a simple DEMO code (Singleton mode can be explained in more detail here ):

Public class Singleton
{
Public static Singleton instance
{
Get
{
Return nested. instance;
}
}
Private class nested
{
Public readonly static Singleton instance = new Singleton ();
}
}
Reflection

Reflection embedded classes must use "+" instead ".". For example, Class A is named insideclass in assembly.

Namespace insideclass
{
Public Class
{
Public class nested
{
Protected void basenested_method ()
{
}
}
}
}

Run

// Succeeded
Object O1 = system. activator. createinstance ("insideclass", "insideclass. A + nested ");
// System. typeloadexception thrown when a failure occurs
Object O2 = system. activator. createinstance ("insideclass", "insideclass. A. nested ");

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.