Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the differences between public, protected and private access specifiers in C#?
Access specifiers in C# control the visibility and accessibility of class members. The three primary access specifiers − public, protected, and private − determine where class members can be accessed from, providing different levels of encapsulation and security.
Syntax
Following is the syntax for declaring members with different access specifiers −
public class ClassName {
public int publicField; // accessible everywhere
protected int protectedField; // accessible in derived classes
private int privateField; // accessible only within this class
public void PublicMethod() { }
protected void ProtectedMethod() { }
private void PrivateMethod() { }
}
Public Access Specifier
The public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
Example
using System;
namespace Demo {
class Rectangle {
public double length;
public double width;
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.length = 7;
r.width = 10;
r.Display();
}
}
}
The output of the above code is −
Length: 7 Width: 10 Area: 70
Protected Access Specifier
The protected access specifier allows a child class to access the member variables and member functions of its base class. Protected members are accessible within the class hierarchy but not from external classes.
Example
using System;
namespace MySpecifiers {
class Demo {
protected string name = "Website";
protected void Display(string str) {
Console.WriteLine("Tabs: " + str);
}
}
class Test : Demo {
static void Main(string[] args) {
Test t = new Test();
Console.WriteLine("Details: " + t.name);
t.Display("Product");
t.Display("Services");
t.Display("Tools");
t.Display("Plugins");
}
}
}
The output of the above code is −
Details: Website Tabs: Product Tabs: Services Tabs: Tools Tabs: Plugins
Private Access Specifier
The private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members from outside the class.
Example
using System;
namespace Demo {
class Rectangle {
private double length;
private double width;
public void AcceptDetails() {
length = 10;
width = 15;
}
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.AcceptDetails();
r.Display();
}
}
}
The output of the above code is −
Length: 10 Width: 15 Area: 150
Comparison of Access Specifiers
| Access Specifier | Same Class | Derived Class | External Class |
|---|---|---|---|
public |
? Accessible | ? Accessible | ? Accessible |
protected |
? Accessible | ? Accessible | ? Not Accessible |
private |
? Accessible | ? Not Accessible | ? Not Accessible |
Conclusion
Access specifiers in C# provide encapsulation by controlling member visibility. Use public for members that need external access, protected for inheritance scenarios, and private to hide implementation details and maintain data integrity within the class.
