mydomain
No ADS
No ADS

Access Modifier in C#

  1. Modifier in Java
  2. Override of access modifiers
  3. private access modifier
  4. private constructor
  5. protected access modifier
  6. internal access modifier
  7. protected internal access modifier
  8. public access modifier
  9. Access modifier and inheritance

1. Modifier in Java

The access modifiers in CSharp specifies accessibility (scope) of a data member, method, constructor or class.

There are 5 types of access modifiers in CSharp:
  • private
  • protected
  • internal
  • protected internal
  • public

2. Override of access modifiers

No ADS
Modifier
Description
private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified
protected
Access is limited to within the class definition and any class that inherits from the class
internal
Access is limited exclusively to classes defined within the current project assembly
protected internal
Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.
public
There are no restrictions on accessing public members.
The table below illustrates give you an overview of how to use the access modifiers.
Same Assembly
Different Assembly
Access Modifier
Access within class
Access within package
Access outside package by subclass only
Access outside package and not in subclass.
private
Y
protected
Y
Y
Y
internal
Y
Y
Y
protected internal
Y
Y
Y
public
Y
Y
Y
Y
Y
You can see more details follow the example below:

3. private access modifier

The private access modifier is accessible only within class.
You cannot access to private member outside the class that defines that member. CSharp will notify error at the compile time of the class.

4. private constructor

Constructor, method, field are known as members of the class.
If you create a class and have a private constructor, you cannot create a object of this class from that private constructor, outside this class. Let's see an illustrative example:

5. protected access modifier

No ADS
protected access modifier: Access is limited to within the class definition and any class that inherits from the class.
The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class, interface...

6. internal access modifier

Access is limited exclusively to classes defined within the current project assembly

An assembly is the compiled output of your code, typically a DLL, but your EXE is also an assembly. It's the smallest unit of deployment for any .NET project.

The assembly typically contains .NET code in MSIL (Microsoft Intermediate language) that will be compiled to native code ("JITted" - compiled by the Just-In-Time compiler) the first time it is executed on a given machine. That compiled code will also be stored in the assembly and reused on subsequent calls.

7. protected internal access modifier

protected internal access modifier is a combination of protected and internal access modifier, the member of the class with this access modifier, you can only access to this member in the class that defines it or subclasses and included in the same Assembly.

8. public access modifier

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

9. Access modifier and inheritance

No ADS
In CSharp, you can override amethod of superclass with a method of the same name and parameters, and return type in the subclass, but you are not allowed to change its access level.
However, you can create a method with the same name, parameters, return type, but different access levels if using the new keyword, the fact that this is a different method unrelated to the method of the parent class.
The difference between the overrideandnew has been explained in the document "Inheritance and polymorphism in CSharp" you can look at:
No ADS
No ADS