A type or type member may control its accessibility to other types and other assemblies using access modifiers.
The following table lists five access modifiers you can use.
Modifier | Description |
---|---|
public | Fully accessible. This is the implicit accessibility for members of an enum or interface. |
internal | Accessible only within the containing assembly or friend assemblies. This is the default accessibility for non-nested types. |
private | Accessible only within the containing type. This is the default accessibility for members of a class or struct. |
protected | Accessible only within the containing type or subclasses. |
protected internal | The union of protected and internal accessibility. |
Class2 is accessible from outside its assembly; Class1 is not:
class Class1 {} // Class1 is internal (default) public class Class2 {}
ClassB exposes field x to other types in the same assembly.
x from ClassA can only be access inside ClassA.
class ClassA { int x; // x is private (default) } class ClassB { internal int x; }
Functions within Subclass can call MyMethod but not Test:
class BaseClass { void Test() {} // Test is private (default) protected void MyMethod() {} } class Subclass : BaseClass { void Test1() { Test(); } // Error - cannot access Test void Test2() { MyMethod(); } // OK }