Nested Classes in C#
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods (member function which defines actions) into a single unit. In C#, a user is allowed to define a class within another class. Such types of classes are known as nested class. This feature enables the user to logically group classes that are only used in one place, thus this increases the use of encapsulation, and create more readable and maintainable code.
Syntax:
class Outer_class {
// Code..
class Inner_class {
// Code..
}
}
Example:
// C# program to illustrate the // concept of nested class using System; // Outer class public class Outer_class { // Method of outer class public void method1() { Console.WriteLine("Outer class method"); } // Inner class public class Inner_class { // Method of inner class public void method2() { Console.WriteLine("Inner class Method"); } } } // Driver Class public class GFG { // Main method static public void Main() { // Create the instance of outer class Outer_class obj1 = new Outer_class(); obj1.method1(); // This statement gives an error because // you are not allowed to access inner // class methods with outer class objects // obj1. method2(); // Creating an instance of inner class Outer_class.Inner_class obj2 = new Outer_class.Inner_class(); // Accessing the method of inner class obj2.method2(); } } |
Output:
Outer class method Inner class Method
Important points:
- A nested class can be declared as a private, public, protected, internal, protected internal, or private protected.
- Outer class is not allowed to access inner class members directly as shown in above example.
- You are allowed to create objects of inner class in outer class.
- Inner class can access static member declared in outer class as shown in the below example:
Example:
// C# program to illustrate the// concept of nested class accessing// static members of the outer classusingSystem;// Outer classpublicclassOuter_class {// Static data member of the outer classpublicstaticstringstr ="Geeksforgeeks";// Inner classpublicclassInner_class {// Static method of Inner classpublicstaticvoidmethod1(){// Displaying the value of a// static member of the outer classConsole.WriteLine(Outer_class.str);}}}// Driver ClasspublicclassGFG {// Main methodstaticpublicvoidMain(){// Accessing static method1// of the inner classOuter_class.Inner_class.method1();}}chevron_rightfilter_noneOutput :
Geeksforgeeks
- Inner class can access non-static member declared in outer class as shown in the below example:
Example:
// C# program to illustrate the// concept of nested class// accessing non-static member// of the outer classusingSystem;// Outer classpublicclassOuter_class {// Non-static data// member of outer classpublicintnumber = 1000000;// Inner classpublicclassInner_class {// Static method of Inner classpublicstaticvoidmethod1(){// Creating the object of the outer classOuter_class obj =newOuter_class();// Displaying the value of a// static member of the outer class// with the help of objConsole.WriteLine(obj.number);}}}// Driver ClasspublicclassGFG {// Main methodstaticpublicvoidMain(){// Accessing static method1// of inner classOuter_class.Inner_class.method1();}}chevron_rightfilter_noneOutput :
1000000
- The scope of a nested class is bounded by the scope of its enclosing class.
- By default, the nested class is private.
- In C#, a user is allowed to inherit a class (including nested class) into another class.
Example:
// C# program to illustrate the// concept inheritanceusingSystem;// Outer classpublicclassOuter_class {// Method of outer classpublicvoidmethod1(){Console.WriteLine("Outer class method");}// Inner classpublicclassInner_class {}}// Derived classpublicclassExclass : Outer_class {// Method of derived classpublicvoidfunc(){Console.WriteLine("Method of derived class");}}// Driver ClasspublicclassGFG {// Main methodstaticpublicvoidMain(){// Creating object of// the derived classExclass obj =newExclass();obj.func();obj.method1();}}chevron_rightfilter_noneOutput :
Method of derived class Outer class method
- In C#, the user is allowed to inherit a nested class from the outer class.
Recommended Posts:
- C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)
- Partial Classes in C#
- C# | Abstract Classes
- How to create 4-ValueTuple in C#?
- How to create 2-ValueTuple in C#?
- Check if two enums are equal or not in C#
- How to compare Enum values in C#?
- How to create 3-ValueTuple in C#?
- How to create 1-ValueTuple in C#?
- How to create 7-ValueTuple in C#?
- ValueTuple in C#
- How to create 6-ValueTuple in C#?
- How to create 5-ValueTuple in C#?
- LINQ | Partition Operator | SkipWhile
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



