In this article, I am going to explain the access modifier in c# . Basically Access modifiers show the visibility or scope of method, property and class. I will discuss each and everything with the help of examples.
Types of Access modifiers,
- Public
- Private
- Protected
- Public protected
- Internal
Public
There are no any restriction on the public members. Public members are accessible from anywhere. Let us see with the help of example.
Where public members are accessible?
- Public members are accessible within the same class
- Public members are accessible from outside of the class.
- Public members are accessible from outside of the assembly(Project) .
Let me show you what is assembly with the help of picture . As we know one solution can have multiple projects, Whenever we compile our application a assembly(.dll) is created inside the bin folder . It means public member of one application can be accessed by another application of solution. You can consider one project as a signal assembly for your basic understanding. It means public member of one project can be accessible by another project by adding reference of project.
Private - Where private members are accessible?
- Private members are accessible within the same class.
Protected - Where protected members are accessible?
- 1. Protected members are accessible within the same class.
- Protected members are accessible inside the child class.
Public protected - Where Public protected members are accessible?
- Public protected members are accessible within same class.
- Public protected members are accessible within the child class.
- Public protected members are accessible from outside of the assembly but class should be as child class.
Internal - Where Internal members are accessible?
- Internal members are accessible within the same class.
- Internal members are accessible by any class within the same assembly (Project).
Remarks
Here meaning of assembly is signal Project to understand, but in the reality project is not a assembly. We compile our web application project one .dll file is created inside the bin folder .This .dll file is called assembly.