Public, protected, private, internal, protected internal brief analysis

Source: Internet
Author: User

Public is the most accessible, such as name, everyone can know someone else's name, this is not a secret

Protected has lower access rights, only subclasses can access the protected property of the parent class. It's like Lao Tzu's property is inherited only by his son. If someone wants to ask: Hey, son. property = How much? Son will refuse, because son's property is inherited from the father, not to vent.

Private access to the lowest, only I can know. Like someone's bank card password. Only his own internal method of the class can access this property.

Internal access is limited to the same assembly. What do you call the same assembly? That is, in the same class library (DLL), the internal property of the class is the same as the public property, which can be accessed by anyone.

The access rights of protected internal are the protected and internal's set. This means that for other classes that belong to the same assembly, you can access him like the internal property, and for classes of different assemblies, only classes that inherit my class can access this property. Note: The order of protected and internal is not important.

Give me a chestnut:

Assembly 1:

public class A

{

Protected internal string x= "X";

}

public class D

{

public string func ()
{

A = new A ();

Return A.x;//ok, you can correctly access the

}

}

Assembly 2: DLL referencing assembly 1

public class B:a

{

public string func ()

{

Return X;//ok, you can correctly access the

}
}

public class C

{

public string func ()

{

A a= new A ();

Return a.x;//no, can not access to

}
}

Public, protected, private, internal, protected internal brief analysis

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.