C# Modifiers (Quick Reference)

Overview of CSharp .NET Access Modifiers

Access Modifiers are used to define the “visibility” of a type (class) or the members of a type. The full list of access modifiers is public, private, protected, internal, and protected internal.

Definitions

Access Modifier Description
public Access is not restricted.
private Access is limited to the containing type.
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.

Notes

If no access modifier is specified for a given class or member definition, the appropriate default access modifier is applied by the compiler. Also, types, nested types, and type members each have a unique subset of access modifiers that can be specified.

Access Modifiers for Types

  • Non-nested Types
    • Implicit default
      • internal
    • Allowable access modifiers
      • public
      • internal
  • Nested Types
    • Implicit default
      • private
    • Allowable access modifiers
      • private
      • protected
      • protected internal

Access Modifiers for Type Members

  • Implicit default
    • private
  • Allowable modifiers
    • public
    • private
    • protected
    • internal
    • protected internal

Code Examples

Default Access Modifiers

class Person //   Implicitly internal
{
     Person()   {}  //   Implicitly private
}
  1. Since no access modifier is specified for the Person type, its accessibility level defaults to internal.
  2. Since no access modifier is specified for the Person() constructor method (a member of the Person type), its accessibility level defaults to private.

Explicitly Defined Access Modifiers

internal class Person // Explicitly internal
{
	private Person() {}  // Explicitly private
}

Here, the default access modifiers for the type and the type’s member are defined explicitly.

Explicitly Defined Access Modifiers

public class Person // Explicitly public
{
	public Person() {}  // Explicitly public
}
  1. The class is made public and is accessible to code from external assemblies.
  2. The constructor method is made public and is accessible from code defined outside of the Person class.

Other Modifiers

Definitions

Modifier Description
abstract Indicates that a class is intended only to be a base class of other classes. Abstract classes cannot be instantiated directly, though classes that derive from an abstract class can be instantiated.
const Specify that the value of the field or the local variable cannot be modified.
override Provide a new implementation of a virtual member inherited from a base class.
readonly Declare a field that can only be assigned values as part of the declaration or in a constructor in the same class.
sealed Specify that a class cannot be inherited.
static Declare a member that belongs to the type itself rather than to a specific object.
virtual Declare a method or an accessor property whose implementation can be changed by an overriding member in a derived class.