For Asp.net Training with C# Click Here
Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com
Access
specifier in C#
Access specifier defines the accessibility of class member.
We can restrict and limit the accessibility of member of classes.
Access
Specifier and their accessibility
Access Spicifier
|
With in class
|
Derived Class
|
Outside Class
|
With in Assembly
|
Public
|
Yes
|
Yes
|
Yes
|
Yes
|
Private
|
Yes
|
No
|
No
|
No
|
Protected
|
Yes
|
Yes
|
No
|
No
|
Internal
|
Yes
|
No
|
No
|
Yes
|
Protected internal
|
Yes
|
Yes
|
No
|
yes
|
Above table defines the accessibility of all access
spicifier. As this table describe that public member can be accessd everywhere
and private accessed only with in class. This create the protection level that except the class member no other can
access the private member of class.
The detail of protected and internal are following:-
Protected
as I define in the table that protected member can only be
accessed with in class and only derived class. These member can not be access
outside the class and derived class.
For example
Create class having method declared as protected
ProtBaseClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class ProtBaseClass
{
//create protected method
protected int sum(int x, int y)
{
return x + y;
}
}
Create the aspx page and
make the object of this class in page load and try to access the protected
member in it.
As we create the object
of class ProtBaseClass and try to access the method but as it declared as
Protected we are not able to access it outside the class.
Now create another class
and derived ProtBaseClass in it. And try to access the protected member of
class in it.
ProtDerivedClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class ProtDerivedClass:ProtBaseClass
{
public int mul()
{
// access the protected method in derived class.
int p= sum(5,6);
return p;
}
}
As we seen that
protected member can be accessed only in the class and in derived class.
Internal
Internal class members
are those member which only accessed in the class and in a same assembly.
Before understand the
internal we have to understand what the assembly is?
Assembly is file which
contain all the deployment detail of a program. It is the executable file
having extention .dll.
Internal classe member
can acess in a same class and same assembly.
For example
Create a class and
declare member as interal
InternalClass
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class InternalClass
{
//create method as internal
internal int sum(int x, int y)
{
return x + y;
}
}
Create aspx page and
create the object of InternalClass and try to access the internal method
As we seen we are not able to access the method sum
declared internal in the class because it can only access in a class and same
assembly.
Now create the another class and try to access the method
in the class.
InternalAccessClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class InternalAccessClass
{
public int mul()
{
// create the object of internal class
InternalClass ic = new
InternalClass();
//access the method sum which is declared as internal
int p= ic.sum(5, 4);
return p;
}
}
In this class we are able to access the internal
member as it comes in same assembly.
I know you are thinking how?
All classes created in app_code and when we create
the assembly of it(publish the project for deployment) it comes under the same
assembly.
Note:-only one assembly created for the app_code.
If we create the class and do and add it in the app_code
then we can not access the internal member in it as it does not come under the
same assembly.
For example
This class is not created in app_code so we are not
able to access the internal member of class.
Protected Internal
Protected internal is hybrid of protected and
internal. We can access these member in protected and in same assembly too.
Hope you enjoyed the Artical.
superhit explanationss wow!!!
ReplyDelete