What are the access-specifiers available in c#?

Answer Posted / kumar vishwajit

1)Public: if we declare any member as public then it can be
accessed from anywhere.
ex: Class Myclass1
{
public int x;
public int y;
}
Class Myclass2
{
public static void main()
{
Myclass1 mc=new Myclass1();
//direct accessing public member
mc.x=10;
mc.y=15;
console.writeline(mc.x,mc.y);
}
}
2)Private: Private members are accessible only within the
body of the class or the struct in which they are declared.
ex:
class Employee
{
public string name = "Jeet";
double salary = 100.00; // private access by default
public double AccessSalary() {
return salary;
}
}

class MainClass
{
public static void Main()
{
Employee e = new Employee();

// Accessing the public field:
string n = e.name;

// Accessing the private field:
double s = e.AccessSalary();
}
}
double s = e.salary; //can not be accessed due to private
lavel....

3)Protected: A protected member is accessible from within
the class in which it is declared, and from within any class
derived from the class that declared this member.

ex:class A
{
protected int x = 123;
}

class B : A
{
void F()
{
A a = new A();
B b = new B();
a.x = 10; // Error
b.x = 10; // OK
}
}
//a.x =10 generates an error because A is not derived from B.
4)internal - The member can only be accessed from type it
originates from or other types in the same assembly
5)it's similar to Protected access
specifier, it also allows a class to hide its member
variables and member function to be accessed from other
class objects and function, excepts child class, within the
application. used while implementing inheritance.

Is This Answer Correct ?    12 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

in the nunit test framework, which attribute must adorn a test class in order for it to be picked up by the nunit gui?

675


How does foreach loop work in c#?

482


Is is possible to force garbage collector to run?

526


What are the advantages of constructor?

457


Explain the process of Serialization?

548






What is the difference between early binding and late binding in c#?

504


What is the function of .IsDescendent()?

580


Can you see a loop recorder?

499


What is the difference between struct and class c#?

486


What are the types of parameters in c#?

530


What's the difference between a static method and a non static method c#?

503


What does static mean in c sharp?

574


Can you inherit from a sealed class?

495


What is the difference between abstract and abstraction?

442


what is scope of a protected internal member variable of a c# class

540