What are the access-specifiers available in c#?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / vakil vijay vamangari
Public: With respect to the assembly the variable,methods of
public class can be access any where .
Private: This is default in c#.net.With respect to the
assembly the variable,methods of private class can be
access only with in that class only.
Protected: With respect to the assembly the variable,methods
of protected class can be access with in that class(base
class) and accessed in class which is derived from that
base class.
Internal(friend in vb.net): With respect to the assembly the
variable,methods of internal class can be access with in
that assembly not the outside of that assembly
Protected Internal: Protected + Internal
With with respect to the assembly the variable,methods
of this class can be access with in that assembly and
protected out side the assembly
Is This Answer Correct ? | 4 Yes | 13 No |
Answer / archana
1. public - The member can be accessed from anywhere
2. internal - The member can only be accessed from type it
originates from or other types in the same assembly
3. protected - The member can only be accessed from the type
it originates from or from detrieved types of the
originating type
4. private - The member is only accessible by other members
within the type it originates from.
Is This Answer Correct ? | 52 Yes | 65 No |
Answer / yogaiah
public,private,protected,internal,protectedinternal,thumbfriend
Is This Answer Correct ? | 23 Yes | 67 No |
What is namespace in oop?
What is difference between string and stringbuffer in c#?
In languages without exception-handling facilities, we could send an error-handling procedure as a parameter to each procedure that can detect errors that must be handled. What disadvantages are there to this method?
What are the fundamental principles of oo programming?
Why do we use dictionary in c#?
What is InterFace???
Why static constructor is parameterless in c#?
What is list collection in c#?
What Are The .Net Object
Can enum be null c#?
HOW to Develope the CRUD(create,read,update,delete) FORM IN WINDOWS form by using c# only
0 Answers Petranics Solutions,
Why do we need properties in c#?