0

Access modifier of c#

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

  1. Public
  2. Private
  3. Protected
  4. Public protected
  5. Internal

 

Public:- There is no any restriction on the public member. Public members are accessible from anywhere. Let us see with the help of an example.

Where public members are accessible?

      1. Public members are accessible within the same class

  1. Public members are accessible from outside of the class.
  2. Public members are accessible from outside of the assembly(Project) .

Let me show you what is assembly with the help of a picture . As we know one solution can have multiple projects, Whenever we compile our application an assembly(.dll) is created inside the bin folder. It means the public member of one application can be accessed by another application of the solution. You can consider one project as a signal assembly for your basic understanding. It means the public member of one project can be accessible by another project by adding the reference of project.

Picture:-1

Private:-

Where private members are accessible?

 

  1. Private members are accessible within the same class.

Protected:-

Where protected members are accessible?

 

  1. Protected members are accessible within the same class.
  2. Protected members are accessible inside the child class.

Public protected:-

Where Public protected members are accessible?

  1. Public protected members are accessible within the same class.
  2. Public protected members are accessible within the child class.
  3. Public protected members are accessible from outside of the assembly but the class should be a child class.

Internal:-

Where Internal members are accessible?

  1. Internal members are accessible within the same class.
  2. Internal members are accessible by any class within the same assembly (Project).

 

Remarks:- Here the meaning of assembly is signal Project to understand, but in the reality project is not an assembly. We compile our web application project one .dll file is created inside the bin folder .This .dll file is called an assembly.

Admin

Leave a Reply

Your email address will not be published. Required fields are marked *