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
Private Methods in C#
Private methods in C# can only be accessed within the class where they are defined. They use the private access modifier to restrict visibility and are commonly used for internal helper functionality that should not be exposed to external code.
The private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only methods within the same class can access private members, providing encapsulation and data hiding.
Syntax
Following is the syntax for declaring a private method −
private returnType MethodName(parameters) {
// method body
}
Using Private Methods for Encapsulation
Private methods are essential for implementing internal logic that should remain hidden from the outside world −
Example
using System;
class Demo {
private int displayOne() {
return 10;
}
public int displayTwo() {
return 10;
}
}
class Program {
static void Main() {
Demo d = new Demo();
// Console.WriteLine(d.displayOne()); // This would cause compilation error
Console.WriteLine(d.displayTwo());
}
}
The output of the above code is −
10
In the above example, we cannot call the private method displayOne(). If we try to call it, a compilation error occurs because private methods are not accessible from outside the class.
Private Methods as Helper Functions
Private methods are commonly used as helper functions to break down complex public methods into smaller, manageable pieces −
Example
using System;
class Calculator {
public double CalculateCircleArea(double radius) {
if (IsValidRadius(radius)) {
return GetAreaFormula(radius);
}
return 0;
}
private bool IsValidRadius(double radius) {
return radius > 0;
}
private double GetAreaFormula(double radius) {
return Math.PI * radius * radius;
}
public void DisplayResult(double radius) {
double area = CalculateCircleArea(radius);
Console.WriteLine($"Area of circle with radius {radius}: {area:F2}");
}
}
class Program {
static void Main() {
Calculator calc = new Calculator();
calc.DisplayResult(5.0);
calc.DisplayResult(-2.0);
}
}
The output of the above code is −
Area of circle with radius 5: 78.54 Area of circle with radius -2: 0
Private Methods Within the Same Class
Private methods can call other private methods within the same class, enabling modular design −
Example
using System;
class BankAccount {
private double balance = 1000;
public void Withdraw(double amount) {
if (ValidateWithdrawal(amount)) {
ProcessWithdrawal(amount);
LogTransaction("Withdrawal", amount);
}
}
private bool ValidateWithdrawal(double amount) {
return amount > 0 && amount <= balance;
}
private void ProcessWithdrawal(double amount) {
balance -= amount;
Console.WriteLine($"Withdrawal successful. Remaining balance: ${balance}");
}
private void LogTransaction(string type, double amount) {
Console.WriteLine($"Transaction logged: {type} of ${amount}");
}
}
class Program {
static void Main() {
BankAccount account = new BankAccount();
account.Withdraw(200);
account.Withdraw(1500);
}
}
The output of the above code is −
Withdrawal successful. Remaining balance: $800 Transaction logged: Withdrawal of $200
Conclusion
Private methods in C# provide encapsulation by hiding internal implementation details from external code. They are essential for creating helper functions, breaking down complex logic, and maintaining clean, modular code while ensuring that sensitive operations remain internal to the class.
