C# Modifiers

C# Modifiers are the keywords used to control the visibility of a method or a class member. C# modifiers can be also used to define the nature of a method or member. Modifiers like private and public controls the visibility whereas virtual or abstract modifiers are used to define the nature.

Following is the list of C# access modifiers and their uses.

Visibility Modifiers

Modifier Applies On Description
public It can be used with Any types or members The item will become visible to other code
protected Any member of a type, and
any nested type
The item is visible only to any derived type.
internal Any types or members The item is visible only within its containing
assembly.
private Any member of a type, and
any nested type
The item is visible only inside the type to which it
belongs.
protected internal Any member of a type, and
any nested type
The item is visible to any code within its containing
assembly and to any code inside a derived type.

Other Modifiers

Modifier Applies On Description
new Applies to Function members Used to hide inherited member with the same signature.
abstract Applies to Function Members Only Used to define the signature of a member with no implementation.
virtual Applies to Function Members Only This keywords indicates that derived class can override this member.
static Applies on all Members If this modifier is used with a Member. The member can not be called with a specific instance.
override Function members only If used with a function member, it overrides an inherited virtual or abstract member.
sealed Applies on Classes, methods, and
properties
If applies on class, then class can not be inherited.
extern Applies on [DllImport]
methods only
If used with a member, then it gives a way to implement the member externally.

Let's learn about the inheritance in the next section.