C# Access Modifiers

All types and type members have an accessibility level. The accessibility level controls whether they can be used from other code in your assembly or other assemblies. An assembly is a .dll or .exe created by compiling one or more .cs files in a single compilation. 

Definitions of Access Modifiers for C#:

  • public: Code in any assembly can access this type or member. The accessibility level of the containing type controls the accessibility level of public members of the type.
  • private: Only code declared in the same class or struct can access this member.
  • protected: Only code in the same class or in a derived class can access this type or member.
  • internal: Only code in the same assembly can access this type or member.
  • protected internal: Only code in the same assembly or in a derived class in another assembly can access this type or member.
  • private protected: Only code in the same assembly and in the same class or a derived class can access the type or member.
  • file: Only code in the same file can access the type or member.

The record modifier on a type causes the compiler to synthesize extra members. The record modifier doesn't affect the default accessibility for either a record class or a record struct.

Summary of Access Modifiers for C#:

Default of Access Modifiers for C#:

  • class --- internal
    • members, including nested class and struct, --- private
  • struct --- internal
    • members, including nested class and struct, --- private
  • delegate --- internal
  • interface --- internal
    • members --- public

References:


Similar Articles