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

How do I format a string in c#?

853


Does c# have a 'throws' clause?

1022


What is sqldataadapter in c#?

906


how to Create a datagridview control with check box column with 8rows in it, the maximum number of check boxes checked should be 3, when user checks the 4th corresponding message should be displayed and check box should be checked. User can uncheck the checked boxes Note: read-only property should not be used

2884


What do you mean by winforms in c#?

969


What is the system namespace?

912


What is difference between list and dictionary in c#?

887


Why are dynamic link library used over static one?

978


What is the use of oops in c#?

912


Can we override main method in c#?

967


What's new in c#?

909


Please explain value types and reference types used in c#?

866


Can you create partial delegates and enumerations?

1023


What is the difference between structure and class in c#?

972


What is default c#?

953