Open In App

Nested Classes in C#

Improve
Improve
Like Article
Like
Save
Share
Report

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 creates more readable and maintainable code.

Syntax:

class Outer_class {

       // Code..

       class Inner_class {

          // Code.. 
       }
}

Example:

C#




// 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#




// C# program to illustrate the
// concept of nested class accessing
// static members of the outer class
using System;
 
// Outer class
public class Outer_class {
 
    // Static data member of the outer class
    public static string str = "Geeksforgeeks";
 
    // Inner class
    public class Inner_class {
 
        // Static method of Inner class
        public static void method1()
        {
 
            // Displaying the value of a
            // static member of the outer class
            Console.WriteLine(Outer_class.str);
        }
    }
}
 
// Driver Class
public class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Accessing static method1
        // of the inner class
        Outer_class.Inner_class.method1();
    }
}


Output

Geeksforgeeks

Inner class can access non-static member declared in outer class as shown in the below example:

Example:

C#




// C# program to illustrate the
// concept of nested class
// accessing non-static member
// of the outer class
using System;
 
// Outer class
public class Outer_class {
 
    // Non-static data
    // member of outer class
    public int number = 1000000;
 
    // Inner class
    public class Inner_class {
 
        // Static method of Inner class
        public static void method1()
        {
            // Creating the object of the outer class
            Outer_class obj = new Outer_class();
 
            // Displaying the value of a
            // static member of the outer class
            // with the help of obj
            Console.WriteLine(obj.number);
        }
    }
}
 
// Driver Class
public class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Accessing static method1
        // of inner class
        Outer_class.Inner_class.method1();
    }
}


Output

1000000

Instance Inner class can access non-static member declared in Outer class as shown in the below example:

Example:

C#




// C# program to illustrate the
// concept of nested class
// accessing non-static member
// of the outer class
using System;
 
// Outer class
public class Outer_class {
 
    // Non-static data
    // member of outer class
    public int number = 2000000;
 
    // Non-static reference to Inner_class
    // instance.
    public Inner_class Inner { get; set; }
 
    // Constructor to establish link between
    // instance of Outer_class to its
    // instance of the Inner_class
    public Outer_class() {
        this.Inner = new Inner_class(this);
    }
 
    // Inner class
    public class Inner_class {
 
        // private field to hold
        // reference to an instance
        // of the Outer_class
        private Outer_class obj;
 
        // constructor that establishes
        // a reference to the Outer_class
        // to use within an Inner_cass instance
        public Inner_class(Outer_class outer)
        {
            obj = outer;
        }
 
        // Method of Inner class
        public void method1()
        {
            // Displaying the value of a
            // member of the outer class
            // with the help of obj
            Console.WriteLine(obj.number);
        }
    }
}
 
// Driver Class
public class GFG {
 
    // Main method
    public static void Main()
    {
        // Create instance of Outer_class
        Outer_class Outer = new Outer_class();
 
        // Accessing static method1
        // of inner class
        Outer.Inner.method1();
    }
}


Output

2000000
  • 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#




// C# program to illustrate the
// concept inheritance
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 {
    }
}
 
// Derived class
public class Exclass : Outer_class {
 
    // Method of derived class
    public void func()
    {
        Console.WriteLine("Method of derived class");
    }
}
 
// Driver Class
public class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Creating object of
        // the derived class
        Exclass obj = new Exclass();
        obj.func();
        obj.method1();
    }
}


Output

Method of derived class
Outer class method

In C#, the user is allowed to inherit a nested class from the outer class. In C#, a nested class is a class that is defined within another class. A nested class can be either a static class or a non-static class. A nested class can have access to the private members of the outer class, which makes it useful for encapsulation and information hiding. It can also be used to group related functionality together in a single class.

Here is an example of a nested class in C#:

C#




public class OuterClass
{
    private int outerVariable = 10;
 
    // Nested class
    public class NestedClass
    {
        public void PrintOuterVariable(OuterClass outer)
        {
            Console.WriteLine("The value of outerVariable is: " + outer.outerVariable);
        }
    }
}
 
// Create an object of the outer class
OuterClass outerObj = new OuterClass();
 
// Create an object of the nested class
OuterClass.NestedClass nestedObj = new OuterClass.NestedClass();
 
// Call a method of the nested class that accesses the private member of the outer class
nestedObj.PrintOuterVariable(outerObj); //


Output:

The value of outerVariable is: 10

In the example above, we define a class called OuterClass that has a private member variable called outerVariable. We then define a nested class called NestedClass that has a method called PrintOuterVariable that takes an instance of the outer class as an argument and prints the value of outerVariable.

We then create an instance of the outer class called outerObj, and an instance of the nested class called nestedObj. We call the PrintOuterVariable method of the nested class and pass outerObj as an argument to access the private member of the outer class.

Note that to access the nested class from outside of the outer class, we need to use the name of the outer class followed by the name of the nested class, separated by a dot (OuterClass.NestedClass).



Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads