Learn About Access Modifier In 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 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.

Access Modifier

Private - Where private members are accessible?

  1. Private members are accessible within the same class.

Protected - Where protected members are accessible?

  1. 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 same class.
  2. Public protected members are accessible within the child class.
  3. Public protected members are accessible from outside of the assembly but class should be as 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 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.

Next Recommended Reading Access Modifier In C# With Example