C# Access modifiers-Specifiers

C# Access modifiers/Specifiers for a class


Access modifier or Access specifier are the keywords of C# used to specify the accessibility to a type and it's members.

Discussion
  1. What is C# Access Modifier for a class
  2. Types of Access Modifiers
  3. Use in real time scenario
  4. Few Interview Question Associated with it
Lets discuss one by one and Understand in deep.

A.What is C# Access Modifiers  and its type?

  1. C# Modifiers is used to describe the accessibility for class and its members.
  2. This provides Security and also control the access level.

B.Types

Access Modifiers/Specifiers are of 5 types.
  1. Private
  2. Protected
  3. Internal
  4. Protected Internal
  5. Public
Private:- Private Access Specifier/Modifier used to make the class Private. But remember we can not declare class as private.
We can create a private class by using Private Constructor.

Private-class,Private-Constructor
Private Access Modifier Or Private Access Specifier

In the above Fig. we create a normal class called "Private-Class" and decorated the class with a private constructor. So the class will be considered as a private class.

Note:-

  1. The default access modifier of a constructor is Private.
  2. The default access modifier of a class variable and method are Private.

Protected:-Protected access modifier used to make the class accessible to only parent class (Inherited class).

Protected Access Modifier | Protected Access Specifier
Protected Access Modifier | Protected Access Specifier

In the above Image we can see Child Class is inheriting from the parent class . So we can access the Protected members of the parent class .

Note:-

  1. It hide class method and class variable from outside class other than parent class.
  2. We can not declare Protected to a class as an access modifier or Access Specifier. 
  3. Protected keyword can be declared to a Class variable and class member . i.e. Protected int my_int
Internal:-Internal make the class accessible to all the class which are associated with the current assembly i.e. in the same project.

Internal-Access-Modifier,Internal-Access-Specifier
Internal Access Modifier,Internal Access Specifier
In the above Image We declared 2 classes. InternalClass1 and InternalClass2. In 1st class I declared Internal to the class and class member where as in second class i did not declared any access modifier to the class.

So the InternalClass1 is accessible outside of the class where as InternalClass2 is not accessible because the access modifier of the variable and method of a class is Private,But be noted that we may or may not declare a class to Internal as this is the default access modifier.

Note :- 
1.The default access modifier of a class is Internal.
2.We may or may not declare Internal keyword before class . For example Internal Class My_class{}.

Protected Internal:- Protected Internal make the class accessible to the same assembly and the Inherited class/Parent class of other assembly.
It is the combination of both protected and Internal.

Lets take an example.

Lets create a class Library as below.

Protected-Internal-Access-Modifier | Protected-Internal-Access-Specifier

We can see we created a class  Protected_Internal_Data and Created a method called Display_Data .
The access modifier of the method is Protected Internal .

Lets access this method from another project as below.

Protected-Internal-Access-Modifier | Protected-Internal-Access-Specifier
Protected Internal Access Modifier | Protected Internal Access Specifier
We can see we can able to access the data of Display_Data() method in different assembly.
Note:- 
1.We can not declare Protected Internal to a class ,but can declare to a method and its variable.
2.Widely used when we wanted to access the inherited class from other Project or Assembly.

Public:- Public makes the class accessible to all the class which are associated with the current assembly or outside the assembly.

So this is open to all the class and all the project.

We can declare public to a Class,method and variable as an access modifiers.

Look at this with an example.

Public-Access-Modifier | Public-Access-Specifier
Public Access Modifier | Public Access Specifier

See the above Image.We declared Public to the class and class members and will try to access to another class .

See the below Image.


Public-Access-Modifier | Public-Access-Specifier
Public Access Modifier | Public Access Specifier

In the above image we can able to access the other assembly class called clsPublic.

Conclusion:-

  1. Access modifier used for restrict the accessibility.
  2. Access modifier helps to secure your class and your class members.
  3. The default access modifier of a class is Internal.
  4. The default modifier of a method, variable and constructor are Private.

Thanks.....................

Share:

No comments:

Post a Comment

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts