C # Nested types

Source: Internet
Author: User
Tags modifiers

1. What is a nested type: a type defined inside a class and struct is called a nested type, for example

1 class Container 2 {3     class Nested 4     {5        Nested () {}6    }7 }

2. Whether the external type is a struct or a class. Nested type adornment defaults is considered private, can be set to other modifiers, the nested type is inaccessible to the external type, and can be set to public.

Class Container{public     class Nested    {        Nested () {}}    }

3. Nested types (or internal types) can access the containing type (or external type). To access the containing type, pass it as a constructor to the nested type, not subject to the modifiers of the outer class, for example:

 Public class A        {            privateint  _a;                        /* Nested class Definitions */            Private class Nesteda            {                public  Nesteda (a A)                {                    9;                }         }}
View Code

 Public class container{publicclass  nested{private  Container  m_ parent;  Public Nested () {}  Public Nested (Container parent) {M_parent=parent; }}}
View Code
 Public classA {Private Static int_aint; Private int_instanceint; Private Static voidAmethod () {Console.WriteLine (_aint); }         Public voidsayit () {Nesteda.method ( This); }        /*Nested class Definitions*/        Private classNesteda { Public Static voidMethod (A a) {//Static Members_aint = -;                Amethod (); //Instance membersA._instanceint =Ten;            A.sayit (); }        }    }
View Code
However, external classes only have access to fields, methods, and properties that have modifiers as public, internal nested classes. Examples are as follows:
 Public classA { Public Static voidAmethod () {//SuccessNesteda.staticmethod (); //Compile ErrorNesteda._int = -; Nesteda ins=NewNesteda (); //Successins.            Method (); //Compile ErrorIns._instanceint = -; }        /*Nested class Definitions*/        Private classNesteda {Private Static int_int; Private int_instanceint;  Public Static voidStaticmethod () {} Public voidMethod () {}}}
View Code

Inherited:

Inheriting classes, which are classes that inherit classes outside the class, can use only public or internal (the same program collection) method of the nested class in the parent class. However, inheriting classes can then define an inline class and nest classes from the inherited parent class. Such as:

 Public classa{/*Nested class Definitions*/            protected classNested {protected Virtual voidBasenested_method () {}}}  Public classc:a{/*Inline class definition*/      protected classc_nested:nested {protected Override voidBasenested_method () {//Rewrite section        }       } }
View Code

Because C is inherited in a, c_nested can inherit the nested class, thereby obtaining the opportunity to override the parent nested class. However, nested must be an inheritable class and accessible (not private, sealed, static).

A nested class can be arbitrarily arbitrary with any data property of an external class, while an external class accesses a nested class with access modifiers only. From this point of view, the nested class is a complement to the external class, which can be better encapsulated by nesting classes, increasing the maintainability and readability of external classes.

A nested type can access private members of the containing type and protected members (including all inherited private members or protected members).

In the preceding declaration, the full name of the class Nested is container.nested. This is the name of the new instance used to create the nested class, as follows:

container.nested nest = new container.nested ();

C # Nested types

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.