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
C# Nested Classes
A nested class is a class declared inside another enclosing class. It is a member of its enclosing class, and nested classes can access private members of their enclosing class. However, the enclosing class cannot directly access private members of the nested class without creating an instance.
Syntax
Following is the syntax for declaring a nested class −
public class OuterClass {
// outer class members
public class NestedClass {
// nested class members
}
}
To create an instance of a nested class from outside the outer class −
OuterClass.NestedClass instance = new OuterClass.NestedClass();
Basic Nested Class Example
The following example demonstrates a simple nested class structure −
using System;
class Outer {
public int outerField = 10;
public class Inner {
public int innerField = 20;
public void Display() {
Console.WriteLine("Inner class method called");
Console.WriteLine("Inner field value: " + innerField);
}
}
public void ShowOuter() {
Console.WriteLine("Outer field value: " + outerField);
}
}
class Program {
static void Main() {
Outer outer = new Outer();
outer.ShowOuter();
Outer.Inner inner = new Outer.Inner();
inner.Display();
}
}
The output of the above code is −
Outer field value: 10 Inner class method called Inner field value: 20
Nested Class Accessing Outer Class Members
A nested class can access private members of its enclosing class when it has a reference to an instance of the outer class −
using System;
class BankAccount {
private double balance = 1000.0;
private string accountNumber = "ACC123";
public class Transaction {
private BankAccount account;
public Transaction(BankAccount acc) {
account = acc;
}
public void ProcessDeposit(double amount) {
account.balance += amount;
Console.WriteLine($"Deposited: ${amount}");
Console.WriteLine($"New Balance: ${account.balance}");
}
public void ShowAccountInfo() {
Console.WriteLine($"Account: {account.accountNumber}");
Console.WriteLine($"Balance: ${account.balance}");
}
}
public Transaction GetTransaction() {
return new Transaction(this);
}
}
class Program {
static void Main() {
BankAccount account = new BankAccount();
BankAccount.Transaction trans = account.GetTransaction();
trans.ShowAccountInfo();
trans.ProcessDeposit(500.0);
}
}
The output of the above code is −
Account: ACC123 Balance: $1000 Deposited: $500 New Balance: $1500
Static Nested Classes
A static nested class cannot access non-static members of the outer class directly −
using System;
class MathOperations {
public static int globalCounter = 0;
public static class Calculator {
public static int Add(int a, int b) {
globalCounter++;
return a + b;
}
public static int Multiply(int a, int b) {
globalCounter++;
return a * b;
}
public static void ShowOperationCount() {
Console.WriteLine($"Total operations performed: {globalCounter}");
}
}
}
class Program {
static void Main() {
int sum = MathOperations.Calculator.Add(10, 20);
int product = MathOperations.Calculator.Multiply(5, 8);
Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Product: {product}");
MathOperations.Calculator.ShowOperationCount();
}
}
The output of the above code is −
Sum: 30 Product: 40 Total operations performed: 2
Key Features of Nested Classes
| Feature | Description |
|---|---|
| Access to Outer Members | Nested classes can access private members of the enclosing class through an instance reference. |
| Encapsulation | Helps organize related classes and improves code structure and readability. |
| Static Nested Classes | Can only access static members of the outer class directly. |
| Instantiation | Use OuterClass.NestedClass syntax to create instances from outside the outer class. |
Conclusion
Nested classes in C# provide a way to logically group related classes together and improve code organization. They can access private members of their enclosing class and are useful for creating helper classes that are closely tied to the functionality of the outer class.
