C # class nesting

Source: Internet
Author: User

The nested class is defined in the class. Classes with embedded classes are called external classes. The Nested classes are classified into static nesting classes and non-static nesting classes.Class is also called an internal class. Nested classes are another type of composite in UML.CodeRepresentation, indicating that the coupling is higher and more closely related to the external class.
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 );
}
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;
}
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 _;
Private class nesteda
{
Public nesteda ()
{
A. _ A = 9;
}
}
}

Inheritance
An inherited class, that is, a class that inherits the external class, can only use the public or internal of the nested class in the parent class (the sameProgramSet) method. However, an inherited class can define another embedded class. and inherit the nested class from the parent class. For example:
Public Class A
{< br> protected class nested
{< br> protected virtual void basenested_method () {}< BR >}< br> Public Class C: A
{< br> protected class c_nested: nested
{< br> protected override void basenested_method ()
{< br> // rewrite part
}< BR >}< br> 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 access any data attribute of the external class at will, while the external class can only access the nested class according to the access modifier. From this perspective, nested classes are complementary to external classes.The Nested classes can get better encapsulation and increase the maintainability and readability of the 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, nested classes begin.Initialization.

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 demonstration code (Singleton ModeYou can have a more detailed explanation here ):

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

 Reflection

 The reflection embedded class must use "+" instead of ".". For example, Class A is in the Assembly named insideclass.

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 ");

Related Article

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.