They are used to define scope of the Class or its Members i,e who can access it and who cannot Access it.
Here we will discuss about each access specifier with an example.
.Net Supports five Access Specifiers.
1 Public.
2 Private.
3 Protected.
4 Internal.
5 Protected Internal.
public:
So any member declared as public can be accessed from the outside the class.
We can access from other class using object reference.
Example
File-New- Project- Console Application.
In the above example we have created a object in Derived Class , since the variable is public it can be accessed from outside the class.
output
private
Members declared as private are not Accessible outside the class.
A class under Namespace cannot be declared as private.
Example
Output:
Example for Public and Private Access Specifier.
output
2 Example
output
Protected
If the members are declared as protected , then they can be accessed only From the child class., it is same as private.
Note: A class under Namespace cannot be declared as private.
This access specifier is used when we need to use Inheritance in the program.
output
Internal
Members are declared as internal under a class than they r accessible only with in the Project either from the Child Class or a Non Child Class. These are accessible only within the Assembly.
internal is the default Access Specifier for the Class.
output
Protected Internal
Members declared as ProtectedInternal under a Class enjoys Dual Scope. i,e with the project they behave as internal members and provide access to all the classes, outside the project they change to Protected and still provide access to there child classes.
Here we will discuss about each access specifier with an example.
.Net Supports five Access Specifiers.
1 Public.
2 Private.
3 Protected.
4 Internal.
5 Protected Internal.
public:
So any member declared as public can be accessed from the outside the class.
We can access from other class using object reference.
Example
File-New- Project- Console Application.
In the above example we have created a object in Derived Class , since the variable is public it can be accessed from outside the class.
output
private
Members declared as private are not Accessible outside the class.
A class under Namespace cannot be declared as private.
Example
Output:
Example for Public and Private Access Specifier.
output
2 Example
output
Protected
If the members are declared as protected , then they can be accessed only From the child class., it is same as private.
Note: A class under Namespace cannot be declared as private.
This access specifier is used when we need to use Inheritance in the program.
output
Another Example of working with Protected Access Specifier.
Internal
Members are declared as internal under a class than they r accessible only with in the Project either from the Child Class or a Non Child Class. These are accessible only within the Assembly.
internal is the default Access Specifier for the Class.
output
Protected Internal
Members declared as ProtectedInternal under a Class enjoys Dual Scope. i,e with the project they behave as internal members and provide access to all the classes, outside the project they change to Protected and still provide access to there child classes.
Output:
Add a new project under the solution of type Console Application, name it as ConsoleApplication2 & rename the default file Program.cs as Four.cs so that class name also changes as Four, now write the following code in it by adding the reference of ConsoleApplication1 assembly from its physical location.
No comments:
Post a Comment
Thank you for visiting my blog