In C#, a nested class is a class that is defined within another class. Nested classes are useful when you want to logically group classes that are only used in one place or when you want to hide a class from other parts of your program except for the enclosing class. Nested classes can access private members of the outer class, which makes them useful in scenarios where tightly-coupled functionality is needed.
In this tutorial, we will explore the concept of nested classes, their use cases, and how they can be applied in C# programming.
A nested class is a class defined within the scope of another class. Nested classes are useful for representing hierarchical relationships between classes or when a class is only relevant in the context of its containing (outer) class. Nested classes have access to all members of the outer class, including private members, which allows for a tightly integrated relationship between the outer and nested class.
private
, public
, protected
, internal
, or protected internal
.class OuterClass
{
private int outerField = 100;
class NestedClass
{
public void DisplayOuterField(OuterClass outer)
{
// Access private member of the outer class
Console.WriteLine(outer.outerField);
}
}
}
In this example, the NestedClass
has access to the outerField
of the OuterClass
, even though outerField
is private
.
Nested classes can have various access modifiers, which determine their visibility from outside the outer class. Here's a quick overview of the types of nested classes:
Let's look at a few examples to understand how nested classes work in C#.
class OuterClass
{
private int outerField = 42;
private class NestedClass
{
public void ShowOuterField(OuterClass outer)
{
Console.WriteLine("Outer field value: " + outer.outerField);
}
}
public void CallNestedClass()
{
NestedClass nested = new NestedClass();
nested.ShowOuterField(this);
}
}
In this example, NestedClass
is private
, so it can only be used within OuterClass
. The method CallNestedClass
creates an instance of NestedClass
and calls the method ShowOuterField
.
Usage:
OuterClass outer = new OuterClass();
outer.CallNestedClass(); // Output: Outer field value: 42
class OuterClass
{
private int outerField = 50;
public class NestedClass
{
public void AccessOuterField(OuterClass outer)
{
Console.WriteLine("Accessing outer field: " + outer.outerField);
}
}
}
In this example, NestedClass
is public
, so it can be accessed from outside the OuterClass
.
Usage:
OuterClass outer = new OuterClass();
OuterClass.NestedClass nested = new OuterClass.NestedClass();
nested.AccessOuterField(outer); // Output: Accessing outer field: 50
class OuterClass
{
protected int outerField = 100;
protected class NestedClass
{
public void DisplayOuterField(OuterClass outer)
{
Console.WriteLine("Outer field: " + outer.outerField);
}
}
}
class DerivedClass : OuterClass
{
public void UseNestedClass()
{
NestedClass nested = new NestedClass();
nested.DisplayOuterField(this);
}
}
In this example, the nested class is protected
and can be accessed from the DerivedClass
.
A good real-world scenario for nested classes is managing hierarchical structures. For example, you might want to implement an organizational structure where each department has employees. A nested class could be used to represent the relationship between departments and employees.
class Department
{
private string departmentName;
public Department(string name)
{
departmentName = name;
}
public class Employee
{
public string EmployeeName { get; set; }
public Employee(string name)
{
EmployeeName = name;
}
public void ShowDetails(Department dept)
{
Console.WriteLine($"Employee {EmployeeName} works in {dept.departmentName} department.");
}
}
}
In this example, the Employee
class is nested inside the Department
class. The Employee
class can access the private departmentName
field of the Department
class.
Usage:
Department department = new Department("HR");
Department.Employee employee = new Department.Employee("John");
employee.ShowDetails(department); // Output: Employee John works in HR department.
This setup allows for logically grouping the Employee
class inside the Department
class and establishing a tight relationship between the two.
private
, public
, protected
, etc.C# nested classes provide a way to encapsulate and tightly couple functionality that only makes sense within the context of another class. They can access the members of their enclosing class, which allows for highly cohesive relationships between classes. Nested classes are not commonly used, but they are powerful in specific use cases such as utility classes within larger structures, organizing complex hierarchical models, or providing helper functionality that should not be accessed by other parts of the program.
By understanding how and when to use nested classes, you can design more modular, efficient, and organized code that accurately represents hierarchical structures or encapsulated behavior. Nested classes are a key tool for maintaining tight relationships within an outer class while controlling accessibility and visibility in your program.