C # Nested classes

Source: Internet
Author: User

Nested classes are defined in the class or structure.

class Container{    class Nested    {        Nested() { }    }}


<1> the default access permission for nested classes is private, which can be set to public, protected, private, internal, and protected internal.
<2> the nested type can access the external class (the class of the nested class). To access the external type, you need to pass the external class to an instance through the constructor.
<3> A nested class can only access static members of an external class, but cannot directly access non-static members of an external class.
Namespace ConsoleApplication11Anonymous {class Class1 {private int x; protected string str; static int y; public class Nested {int xx; string ss; void print () {// int y = x; // error, cannot access external non-static member int z = y; // OK, can access external static member} public Nested (Class1 A) {xx =. x; // access an external class private member ss =. str; // access the external class protection member through an external class instance} class Program {static void Main (string [] args) {Class1 X = new Class1 (); class1.Nested CN = new Class1.Nested (X );}}}


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.