Pages

Friday, May 15, 2009

Accessibility Levels (C# Reference)

Use the access modifiers, public, protected, internal, or private to specify one of the following declared accessibilities for members.

Declared accessibility
Meaning

public

Access is not restricted.

protected

Access is limited to the containing class or types derived from the containing class.

internal

Access is limited to the current assembly.

protected internal

Access is limited to the current assembly or types derived from the containing class.

private

Access is limited to the containing type.

Only one access modifier is allowed for a member or type, except when using the protected internal combination.

Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.

Depending on the context in which a member declaration takes place, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default accessibility is used.

Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.

Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.

Members of
Default member accessibility
Allowed declared accessibility of the member

enum

public

None

class

private

public

protected

internal

private

protected internal

interface

public

None

struct

private

public

internal

private

The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 3.5.1 Declared Accessibility

  • 3.5.3 Protected access for instance members

  • 3.5.4 Accessibility constraints

  • 10.2.3 Access Modifiers

  • 10.2.6.2 Declared Accessibility

See Also

Reference
C# Keywords
Access Modifiers (C# Reference)
Accessibility Domain (C# Reference)
Restrictions on Using Accessibility Levels (C# Reference)
Access Modifiers (C# Programming Guide)
public (C# Reference)
private (C# Reference)
protected (C# Reference)
internal (C# Reference)
Concepts
C# Programming Guide
Other Resources
C# Reference