C# Access Modifiers
Access Modifiers are keywords that define the accessibility of a member, class
or data type in a program. These are mainly used to restrict unwanted data
manipulation by external programs or classes. These modifiers control the
scope and extent of access to classes, methods, properties, and other
members within and from external classes or assemblies. By using modifiers
we ensure that certain data or functionality is exposed only when necessary
otherwise keep it hidden.
1. Public Modifier
public modifier provides access to the entire program. It means that another
method or another assembly which contains the class reference can access
these members or types. This access modifier has the most permissive access
level in comparison to all other access modifiers.
using System;
class Student
{
public int rollNo;
public string name;
public Student(int r, string n)
{
rollNo = r;
name = n;
}
public int getRollNo()
{
return rollNo;
}
public string getName()
{
return name;
}
}
class person
{
static void Main(string[] args)
{
Student S = new Student(1, "XYZ");
// accessible through another method
[Link]("Displaying using class members");
[Link]("Roll number: {0}", [Link]);
[Link]("Name: {0}", [Link]);
[Link]();
[Link]("Displaying Using methods");
[Link]("Roll number: {0}", [Link]());
[Link]("Name: {0}", [Link]());
}
}
Output
Displaying using class members
Roll number: 1
Name: XYZ
Displaying Using methods
Roll number: 1
Name: XYZ
2. private modifier
private access modifier limits access to members of a class so that only the
class itself can use them. Private members cannot be accessed from outside
the class, not even by derived classes.
Example:
using System;
class Parent
{
private int value;
// value is Accessible
// only inside the class
public void setValue(int v)
{
value = v;
}
public int getValue()
{
return value;
}
}
class Child : Parent
{
public void showValue()
{
// Trying to access value
// Inside a derived class
// [Link]( "Value = " + value );
// Gives an error
}
}
class person
{
static void Main(string[] args)
{
Parent obj = new Parent();
// [Link] = 5;
// Also gives an error
// Use public functions to assign
// and use value of the member 'value'
[Link](4);
[Link]("Value = " +
[Link]());
}
}
Output
Value = 4
3. protected Modifier
protected access modifier allows members to be accessible within the class
and by derived classes (subclasses). It does not allow access from outside the
class or its derived classes.
Example:
using System;
class Student
{
protected string name;
public Student(string studentName)
{
name = studentName;
}
}
class person : Student
using System;
class Student
{
protected string name;
public Student(string studentName)
{
name = studentName; // Initialize name
}
}
class person : Student
{
public person(string x) : base(y)
{
public string GetName()
{
// Access protected member from base class
return name;
}
}
class person
{
static void Main(string[] args)
{
person obj = new person("SDM");
// Access the protected member through the derived class method
[Link]("The student's name is: {0}",
[Link]());
}
}
{
public person(string x) : base(y)
{
public string GetName()
{
// Access protected member from base class
return name;
}
}
class person
{
static void Main(string[] args)
{
person obj = new person("SDM");
// Access the protected member through the derived class method
[Link]("The student's name is: {0}",
[Link]());
}
}
Output
The student's name is: SDM