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 is the scope of a private member variable of a class in C#?
The scope of a private member variable in C# is limited to the class in which it is declared. Only methods and properties within the same class can directly access private members. This implements the principle of data encapsulation, which hides internal implementation details from external code.
Syntax
Following is the syntax for declaring private member variables −
private dataType variableName;
Private members can only be accessed by methods within the same class −
class ClassName {
private int value;
public void SetValue(int newValue) {
value = newValue; // Valid - same class
}
}
Scope Rules for Private Members
Same class access: Methods within the same class can read and modify private members.
External class restriction: Other classes cannot directly access private members.
Inheritance limitation: Even derived classes cannot directly access private members of the base class.
Object instances: Private members are accessible to all instances of the same class within class methods.
Example - Private Members Within Same Class
using System;
namespace RectangleApplication {
class Rectangle {
//private member variables
private double length;
private double width;
public void AcceptDetails() {
length = 10; // Accessible - same class
width = 14; // Accessible - same class
}
public double GetArea() {
return length * width; // Accessible - same class
}
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();
// r.length = 5; // ERROR - Cannot access private member
}
}
}
The output of the above code is −
Length: 10 Width: 14 Area: 140
Example - Accessing Private Members Across Instances
Private members can be accessed between different instances of the same class within class methods −
using System;
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void TransferTo(BankAccount other, double amount) {
if (this.balance >= amount) {
this.balance -= amount; // Access own private member
other.balance += amount; // Access other instance's private member
Console.WriteLine("Transfer successful");
}
}
public void ShowBalance(string name) {
Console.WriteLine("{0} Balance: ${1}", name, balance);
}
}
class Program {
static void Main(string[] args) {
BankAccount account1 = new BankAccount(1000);
BankAccount account2 = new BankAccount(500);
account1.ShowBalance("Account 1");
account2.ShowBalance("Account 2");
account1.TransferTo(account2, 200);
account1.ShowBalance("Account 1");
account2.ShowBalance("Account 2");
}
}
The output of the above code is −
Account 1 Balance: $1000 Account 2 Balance: $500 Transfer successful Account 1 Balance: $800 Account 2 Balance: $700
Comparison of Access Modifiers
| Access Modifier | Same Class | Derived Class | External Class |
|---|---|---|---|
private |
? Yes | ? No | ? No |
protected |
? Yes | ? Yes | ? No |
public |
? Yes | ? Yes | ? Yes |
Conclusion
Private member variables in C# have class-level scope, meaning they can only be accessed by methods within the same class. This encapsulation protects internal data from external modification and helps maintain code integrity and security.
