Difference Between Public, Private, Protected and Internal in C# – Programming, Pseudocode Example, C# Programming Example
C#

Difference Between Public, Private, Protected and Internal in C#

What is the difference between Public, Private, Protected and Internal?

There are five types of access specifiers in c# public, private, protected, internal and protected internal. In this article, I have explained each access specifier with an example.

1) Public

– No restrictions to access.

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

– Most common access specifier in C#.

From above example you can see num1 can directly accessible by sample object.

2) Private

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

– Access is limited to within the class definition and any class that inherits from the class.

As num2 is a private variable, It is not accessible by object of sample class

3) 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.

If we define variable as a private variable then compile time error will occur.

4) Internal

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

– It is the default access specifiers for a class in C# programming.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.