All types and type members have accessibility levels to control whether they can be used in other code in your assembly or in other assemblies. You can use the access modifier to specify the accessibility of a type or member when declaring a type or member.
-
-
Public
-
-
Any other code in the same assembly, or other assemblies that reference the assembly, can access the type or member.
-
-
Private
-
-
Only code in the same class or struct can access the type or member.
-
-
Protected
-
-
Only types or members can be accessed by code in the same class or struct, or in a derived class of this class.
-
-
Internal
-
-
Any code in the same assembly can access the type or member, but the code in the other assembly is not available.
-
-
protected internal
-
-
A type or member that is accessible by any code in a class that is derived from its declared assembly or another assembly. access from another assembly must occur in the class declaration, which derives from the class in which the protected inner element is declared, and must occur through an instance of the derived class type.
The following example shows how to specify an access modifier for a type and member:
C#
public class bicycle{Public void pedal () {}}
Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is limited by the accessibility of its containing type. The following sections provide more detailed information about accessibility.
Accessibility of classes and structs
If you do not specify an access modifier, the default is internal.
struct members, including nested classes and structs, can be declared as public, internal, or private. Private nested types cannot be accessed from outside the containing type.
b this derives from an internal class a. " In other words, there cannot be a public class b derived from the inner class a . a public, because all protected or internal members of Span Class="code">a is accessible from the derived class. " If this is allowed, the a becomes a public class, because all protected members or internal members of a can be accessed from derived classes.
You can use InternalsVisibleToAttribute to enable some other assemblies to access your internal types. For more information, see Friend Assemblies (C # and Visual Basic).
Accessibility of class members and struct members
You can declare class members (including nested classes and structs) using any of the five types of access. Struct members cannot be declared as protected members because the structure does not support inheritance.
Typically, the accessibility of a member is lower than the type of the containing member. If a member implements an interface method or overrides a virtual method that is already defined in a public base class, the public members of the inner class can be accessed from an external assembly.
The type of the field, property, or event of any member must be at least as accessible as the member itself.Similarly, the return type and parameter type of any member that is a method, indexer, or delegate must have at least the same accessibility as the member itself.m that returns a class c unless c is also public. " For example, if c is not a public class, you cannot return the class m. a if a is declared as P Rivate. " Also, if a is declared private, then the type a cannot have protected properties.
User-defined operators must always be declared as public operators. For more information, see Operators (C # Reference).
Destructors cannot have accessibility modifiers.
To set the access level for a class member or struct member, add the appropriate keyword to the member declaration, as shown in the following example.
C#
//Public class:public class tricycle{//protected method:protected void pedal () {}//private field: private int wheels = 3; protected internal property:protected Internal int Wheels {get {return Wheels;} }}
| note |
|
The protected internal accessibility level means protected "or" internal, not protected "and" internal. " |
Other Types
When you declare directly with a namespace, you can declare an interface as either a public interface or an internal interface, and the interface defaults to internal accessibility, just like classes and structs. interface members are always public members, because the purpose of an interface is to allow other types to access a class or struct. access modifiers cannot be applied to interface members.
Enumeration members are always public and cannot be applied with any access modifiers.
Delegate behavior is similar to classes and structs. by default, they have internal access when they are declared directly in a namespace, and have private access when nested.
Access modifiers, namespaces