What are the access-specifiers available in c#?

Answers were Sorted based on User's Feedback



What are the access-specifiers available in c#?..

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

What are the access-specifiers available in c#?..

Answer / raees khan

public, private,protected are A-S in c#

Is This Answer Correct ?    2 Yes 3 No

What are the access-specifiers available in c#?..

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

What are the access-specifiers available in c#?..

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

What are the access-specifiers available in c#?..

Answer / yogaiah

public,private,protected,internal,protectedinternal,thumbfriend

Is This Answer Correct ?    23 Yes 67 No

What are the access-specifiers available in c#?..

Answer / swapna

Public,Private,Protected ,Friendly,protected friendly
are the access specifiers in c#.

Is This Answer Correct ?    37 Yes 182 No

Post New Answer

More C Sharp Interview Questions

What?s the difference between the Debug class and Trace class?

2 Answers   Siebel,


In gridview in editmode if we want to display information in one combobox based on

0 Answers   FD, Microsoft,


boxing means converting value type to reference type and unboxing means converting reference type to value type.why we need boxing and unboxing?

3 Answers   General Mills, Value Labs,


Are objects passed by reference in c#?

0 Answers  


What is verbatim string?

0 Answers  






What is array class in c#?

0 Answers  


What is the difference between CONST and READONLY?

0 Answers   BirlaSoft,


What is sealed class in c#?

0 Answers  


Explain briefly the difference between value type and reference type?

0 Answers   Accenture,


what is bubbled event ? give suitable example in code vice with demo

2 Answers  


what optimizations does the c# compiler perform when you use the /optimize+ compiler option?

0 Answers   4Cplus,


What is sqldatareader c#?

0 Answers  


Categories