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
Local Inner Class in C#
A nested class in C# is a class declared inside another enclosing class. The nested class is a member of its outer class and can access the outer class's private members, while the outer class cannot directly access the nested class's members without creating an instance.
Nested classes provide better organization and encapsulation by grouping related functionality together. They are particularly useful when a class is only meaningful within the context of another class.
Syntax
Following is the syntax for declaring a nested class −
class OuterClass {
// outer class members
public class NestedClass {
// nested class members
}
}
To create an instance of the nested class −
OuterClass.NestedClass instance = new OuterClass.NestedClass();
Key Rules of Nested Classes
-
Nested classes can access all members (including private) of the outer class.
-
Outer classes cannot directly access nested class members without creating an instance.
-
Nested classes can have any access modifier:
public,private,protected, orinternal. -
Multiple levels of nesting are allowed.
Basic Nested Class Example
using System;
class OuterClass {
public int outerField = 10;
private int privateField = 20;
public class NestedClass {
public int nestedField = 30;
public void AccessOuter(OuterClass outer) {
Console.WriteLine("Outer field: " + outer.outerField);
Console.WriteLine("Private field: " + outer.privateField);
}
}
}
class Program {
public static void Main() {
OuterClass outer = new OuterClass();
OuterClass.NestedClass nested = new OuterClass.NestedClass();
Console.WriteLine("Nested field: " + nested.nestedField);
nested.AccessOuter(outer);
}
}
The output of the above code is −
Nested field: 30 Outer field: 10 Private field: 20
Nested Class with Different Access Modifiers
using System;
class Computer {
private string brand = "Dell";
public class Processor {
public string type = "Intel i7";
public void ShowDetails(Computer comp) {
Console.WriteLine("Computer: " + comp.brand);
Console.WriteLine("Processor: " + type);
}
}
private class Memory {
public int size = 16;
}
public void CreateComponents() {
Memory mem = new Memory();
Console.WriteLine("Memory: " + mem.size + "GB");
}
}
class Program {
public static void Main() {
Computer comp = new Computer();
Computer.Processor proc = new Computer.Processor();
proc.ShowDetails(comp);
comp.CreateComponents();
}
}
The output of the above code is −
Computer: Dell Processor: Intel i7 Memory: 16GB
Static Nested Class
using System;
class MathUtils {
public static class Calculator {
public static int Add(int a, int b) {
return a + b;
}
public static int Multiply(int a, int b) {
return a * b;
}
}
}
class Program {
public static void Main() {
int sum = MathUtils.Calculator.Add(5, 3);
int product = MathUtils.Calculator.Multiply(4, 6);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Sum: 8 Product: 24
Common Use Cases
-
Helper classes − When a class is only used within another class's context.
-
Event handlers − Organizing event-related classes within their parent class.
-
Data structures − Implementing nodes for linked lists, trees within the main class.
-
Utility classes − Static nested classes for related utility methods.
Conclusion
Nested classes in C# provide better code organization and encapsulation by grouping related functionality together. They can access all members of their outer class while maintaining their own scope and access control, making them ideal for helper classes and tightly coupled components.
