C # Public, private, protected, internal, protected internal & (2010-09-22 13:33:45) Reprint Tags: Category: C #
In the C # language, there are five types of access modifiers: public, private, protected, internal, protected internal. The scope of action is as follows:
Access modifier description
Public access. Not subject to any restrictions.
Private access. This class is restricted to access by members, subclasses, and instances.
protected protected Access. This class and subclass access is limited, and instances cannot be accessed.
Internal internal access. Access is limited to this item and cannot be accessed by others.
protected internal internal protection access. Limited to this item or subclass access, other inaccessible
The editable and default modifiers for C # member types are as follows:
Member type default modifier can be modifier
Enum Public None
class private public, protected, internal, private,
protected internal
Interface Public None
struct private public, internal, private
Let me take a look at the scope of public, private, protected, internal, and protected internal in conjunction with an example.
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceAccessModifier { Public classaccessmodifierclass{ Public stringgetpublicstring () {return "Public String"; } protected stringgetprotectedstring () {return "Protected String";} Private stringgetprivatestring () {return "Private String"; } Internal stringgetinternalstring () {return "Internal String"; }protected Internal stringgetprotectedinternalstring () {return "Protected Internal String"; } voidAvailableaccessmodifier () { This. Getpublicstring (); This. Getprivatestring (); This. Getinternalstring (); This. Getprotectedinternalstring (); This. Getprotectedstring (); } } Public classTestAccessModifierClass1 {voidAvailableaccessmodifier () {Accessmodifierclass Item=Newaccessmodifierclass (); item. Getpublicstring (); item. Getinternalstring (); Item. Getprotectedinternalstring (); } } Public classTestaccessmodifierclass2:accessmodifierclass {voidAvailableaccessmodifier () {Accessmodifierclass Item=Newaccessmodifierclass (); item. Getpublicstring (); Item. Getinternalstring (); Item. Getprotectedinternalstring (); Base. Getprotectedstring ();}} }
Accessmodifierclass is our access modifier class, which has five access modifier methods, visible in the Accessmodifierclass class Availableaccessmodifier () method can access all methods.
The Availableaccessmodifier () method in the TestAccessModifierClass1 class can only access the public, internal, and protected internal methods.
The TestAccessModifierClass2 class inherits from the Accessmodifierclass class, so its Availableaccessmodifier () method can access the public,internal, protected and protected internal methods.
C # Public, private, protected, internal, protected internal (GO)