Understanding C# Access Modifiers

public : public items have no access restrictions. A public member can be accessed from an object, as well as any derived class. A public type can be accessed from other external assemblies.

private : private items can be accessed only by the class that defines the item.

protected : protected items can be used by the class that defines it and any child class. They can not be accessed from outside the inheritance chain.

internal : Internal items are accessible only within the current assembly. Other assemblies can be explicitly granted permission to see the internal items.

protected internal : When the protected and internal keywords are combined on an item, the item is accessible within the defining assembly, within the defining class, and by derived classes inside or outside of the defining assembly.

private protected (new 7.2) : When the private and protected keywords are combined on an item, the item is accesible within the defining class and by derived classes in the same assembly.

Using the Default Access Modifier

By default, type members are implicitly private, while types are implicitly internal.

// Implicity
class Radio
 {
     Radio() { }
 }
     
// Explicitly
internal class Radio
 {
     private Radio() { }
 }

Using Access Modifiers and Nested Types

Nested type is a type declared directly within the scope of class or structure. private,protected,protected internal, and private protected can be applied to a nested type.


public class SportsCar
 {
     private enum CarColor
     {
         Red,
         Green,
         Blue
     }
 }
 
// Error! Non-nested type cannot be marked as private
private class SportsCar
 {
}

Non-nested types can be defined only with the public or internal modifier.