Access Modifiers In C#


It’s been always a confusion about the default access modifiers. Here is a small article which will make things clear.

Access Modifiers in C#:

public

The type or member can be accessed by any other code in the same assembly or another assembly.

private

The type or member can be accessed only by code in the same class or struct.

protected

The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

Default Access Modifiers in C#:

1. A class has default modifiers as Internal.

2. An enum has default modifier as public.

3. An interface has default modifier as public

4. Like class, A struct has default modifier as Internal.

5. A methods, fields, and properties has default access modifier as “Private” if no modifier is specified.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s