C # difficulties (5): Class access types

Source: Internet
Author: User

When talking about the differences between the four categories, it is necessary to explain a concept: Program Set. The program set (assembly.pdf can be simply translated into the. dllor. EXE file generated after compilation. Compared with namespace, it is the logical organization form of the class library. It can be called the physical organization form of the program class library, on msdn, the description "Assembly contains metadata that describes their own version numbers and details of all the data and object types they contain ". Generally, an assembly can contain one or more namespaces.

Public: visible to any method of any other class;
PRIVATE: the members defined in this class can only access the methods of this class;
Protected: only the methods of this class and the methods that inherit the class can be accessed by members of this class;
Internal: Members of this class can only access the methods of any class in the Assembly where the class is located;
Protected internal: the Union of protected and internal.
Therefore, the access level should be public> protected internal> protected> private.

The default modifier of the class is internal, the default modifier of the member variables in the class is private, and namespace can be considered as public. In addition, the access level of the inherited class cannot be higher than the access level of the inherited class. For example, the internal class cannot inherit the private class.CopyCodeThe Code is as follows: using system;

/******************************
* Chapter: C # difficulties one by one (5)
* Author: Wang Hongjian
* Date: 2010-1-15
* Blog: http://www.51obj.cn/
* Email: walkingp@126.com
* Description: access modifier
****************************/
Namespace testmain
{
Public Class
{
Public void alert ()
{
}
}

Internal Class B:
{
Internal new void alert ()
{
Console. writeline ("you ");
}
}

Class Program
{
Static void main ()
{
B = new B ();
B. Alert ();

}
}
}

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.