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

I was trying to use an out int parameter in one of my functions. How should I declare the variable that I am passing to it?

574


What is the use of main method in c#?

492


Explain about finalize method?

552


What is the size of a decimal?

621


What is default access modifier for class in c#?

501






Why do we need events in c#?

547


What are the properties in c#?

493


Is enum a value type c#?

530


What are the variables in c#?

502


What are the 2 types of data types available in c#?

497


what is a constructor? What is a destructor?

526


What is the difference between parse and tryparse in c#?

494


What is the use of return in c#?

490


What is lazy t?

510


How would you describe encapsulation in c#?

486