Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What are the access-specifiers available in c#?

Answer Posted / banipada mandal

One of the goal of object oriented programming is data
hiding. That is a class may be designed to hide its members
from outside accessibility. An access specifier defines
the scope of a class member. A class member refers to the
variables and functions in a class. A program can have many
classes. All C# types and type members have an
accessibility level . We can control the scope of the
member object of a class using access specifiers. We are
using access modifiers for providing security of our
applications.
C# supports five types of access specifiers to tell the
extent of visibility of a class member. They are:-
1. public,
2. private,
3. protected,
4. internal and
5. protected internal.
public:
public is the most common access specifier in C#. It can be
access from anywhere, that means there is no restriction on
accessibility. The scope of the accessibility is inside
class as well as outside. i.e, within containing classes,
Derived classes, containing program, anywhere outside the
containing program.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace @public
{

class Program
{
class publicinclass
{
public int b;
public void xyz()
{
Console.WriteLine("This is also public");

}
}
static void Main(string[] args)
{
publicinclass obj1 = new publicinclass();
obj1.b = 400;
Console.WriteLine("the value of b= " + obj1.b );
obj1.xyz();

derivedpublicaccess dr = new derivedpublicaccess
();
dr.pm = 20;
Console.WriteLine("the value of pm is " +
dr.pm);
dr.drdisplay();

dr.a = 600;
Console.WriteLine("the value of a = " + dr.a);
dr.abc();


}
}
class accesspublic
{
public int a;
public void abc()
{

Console.WriteLine("this is public ");
}
}
class derivedpublicaccess:accesspublic
{
public int pm;
public void drdisplay()
{
Console.WriteLine("also public from derive
class");
}
}
}

private:
The private access specifier in C# is just opposite to the
public access specifier. That is it allows a class to hide
its member variables and member functions from other class
objects and functions. So it is not visible outside the
class. By default, the access specifier is private; if
public, private or protected is not specified.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace @private
{
class Program
{
static void Main(string[] args)
{
baseprivate bbpp = new baseprivate();
// bbpp.b = 30; // b is also private
// bbpp.p = 60; // p is private
bbpp.a = 500; // a is public, so a is
accessible................
Console.WriteLine("the value of a = " + bbpp.a);
bbpp.abc(); // abc() is public, so abc() is
accessible................

derivedprivate drpr = new derivedprivate();
drpr.z = 600;
Console.WriteLine(" the value of z= " + drpr.z);
drpr.d_abc();


}
}
class baseprivate
{
public int a;
int b;
private int p;
public void abc()
{
Console.WriteLine("this is public from base
class");
}
private void xyz()
{
Console.WriteLine("this is private from base
class");
}
}
class derivedprivate : baseprivate
{
public int z;
int x;
private int r;
public void d_abc()
{
Console.WriteLine("this is public from derived
class");
}
private void d_xyz()
{
Console.WriteLine("this is private from derived
class");
}
}
}

protected: The scope of accessibility is limited within the
class or struct and the class derived (Inherited )from this
class. protected member can be access within containing
classes and Derived classes.
internal: Internal member can be access within Containing
classes and Containing program and also access within the
same assembly level but not from another assembly.
protected internal: Protected internal is the same access
levels of both protected and internal. Protected Internal
member is access within containing classes, Derived classes
and containing program. It can access anywhere in the same
assembly and in the same class also the classes inherited
from the same class.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prtotected_internal_access_specifier
{
class Program
{
static void Main(string[] args)
{
A aa = new A();
aa.pi = 100;
Console.WriteLine("protected internal pi from
base class " + aa.pi);

B bb = new B();
bb.pi = 600;
bb.xyz();
bb.abc(); // access from base class abc() from
derive class
}
}
class A
{
protected internal int pi;
public void abc()
{
Console.WriteLine("protected internal pi from
base class through base class function " + pi);
}
}
class B : A
{
public void xyz()
{
Console.WriteLine("protected internal pi from
base class through derived class function " + pi);
}
}
}


You can easily understand all modifiers with the help of
Diagram which is given below.
See it:- Visibility of class member:

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the Types of optimization and name a few and how do u do?

937


What is arraylist c#?

870


What is boxing? Explain its functionality?

916


Difference between a sub and a function in c#.

925


Why c# is called type safe language?

784


Explain the various types of classes used in c#?

882


How do you implement thread synchronization in c#?

840


Difference between abstract classes and interfaces

946


What is thread pooling?

914


Explain the difference between access specifier and access modifier in c#?

915


Why do we use reflection in c#?

883


Explain what a diffgram, and a good use for one Define diffgram? How it be used?

946


How do I edit a dll file?

823


Why do we need oops in c#?

868


Can I fly with a loop recorder?

859