Home  >  Article  >  Backend Development  >  C# difficulties are broken down one by one (5): Class access types

C# difficulties are broken down one by one (5): Class access types

黄舟
黄舟Original
2016-12-22 14:11:301219browse

When talking about the differences between these four categories, it is necessary to explain a concept: assembly. Assembly (Assembly) can be simply understood as a .dll or .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. MSDN describes "assembly" Contains metadata describing their own build numbers and details of all data and object types they contain". The general situation is that an assembly can contain one or more namespaces.

Public: Any method of any other class is visible;
Private: The members defined in this class can only be accessed by the methods of this class;
Protected: The members of this class can only be accessed by the methods of this class and inherit this class Methods of the class can only be accessed;
internal: Members in this class can only be accessed by methods of any class in the assembly where this class is located;
protected internal: is the union of protected and internal.
So the access level from high to low should be Public>protected internal>internal>Protected>Private

The default modifier of the class is internal, the default modifier of the member variables in the class is private, and the namespace can be regarded as the modifier is public. Another point mentioned on msdn is that the access level of the inheriting class cannot be greater than the access level of the inherited class. For example, an internal class cannot inherit a private class.

using System; 

/****************************** 
* Chapter:C#难点逐个击破(五) 
* Author:王洪剑 
* Date:2010-1-15 
* Blog:http://www.51obj.cn/ 
* Email:walkingp@126.com 
* Description:访问修改符 
* ***************************/ 
namespace TestMain 
{ 
public class A 
{ 
public void Alert() 
{ 
} 
} 

internal class B:A 
{ 
internal new void Alert() 
{ 
Console.WriteLine("you"); 
} 
} 

class Program 
{ 
static void Main() 
{ 
B b=new B(); 
b.Alert(); 

} 
} 
}

The above is the content of C# difficulties one by one (5): Class access type. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn