All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies. You specify the accessibility of a type or member when you declare it by using one of these access modifiers:
- public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
- private
The type or member can only be accessed by code in the same class or struct.
- protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.
- internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
- protected internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
The following examples demonstrate how to specify access modifiers on a type and member:
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 constrained by the accessibility of its containing type. The following sections provide more details about accessibility.
Class and Struct Accessibility
Classes and structs that are declared directly within a namespace (in other words, they are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified. Nested classes and structs may also be declared as private. Private nested types are not accessible from the containing type.
Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.
You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. For more information, see Friend Assemblies (C# Programming Guide).
Class and Struct Member Accessibility
Class members (including nested classes and structs) can be declared with any of the five types of access. Struct members cannot be declared as protected because structs do not support inheritance.
The accessibility of a member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal type has only internal accessibility.
When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private.
User-defined operators must always be declared as public. For more information, see operator (C# Reference).
Destructors cannot have accessibility modifiers.
To set the access level for a class or struct member, add the appropriate keyword to the member declaration. Here are some examples:
// 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 means protected OR internal, not protected AND internal. In other words, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected. |
Interfaces declared directly with a namespace can be declared as public or internal and like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.
Enumeration members are always public, and no access modifiers can be applied.
By default, delegates have internal access.
C# Language Specification
For more information, see the following sections in the C# Language Specification:
3.5 Member Access
10.3.5 Access Modifiers
Concepts
Reference