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#
using System;
public class Outer_class {
public void method1()
{
Console.WriteLine( "Outer class method" );
}
public class Inner_class {
public void method2()
{
Console.WriteLine( "Inner class Method" );
}
}
}
public class GFG {
static public void Main()
{
Outer_class obj1 = new Outer_class();
obj1.method1();
Outer_class.Inner_class obj2 =
new Outer_class.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#
using System;
public class Outer_class {
public static string str = "Geeksforgeeks" ;
public class Inner_class {
public static void method1()
{
Console.WriteLine(Outer_class.str);
}
}
}
public class GFG {
static public void Main()
{
Outer_class.Inner_class.method1();
}
}
|
Inner class can access non-static member declared in outer class as shown in the below example:
Example:
C#
using System;
public class Outer_class {
public int number = 1000000;
public class Inner_class {
public static void method1()
{
Outer_class obj = new Outer_class();
Console.WriteLine(obj.number);
}
}
}
public class GFG {
static public void Main()
{
Outer_class.Inner_class.method1();
}
}
|
Instance Inner class can access non-static member declared in Outer class as shown in the below example:
Example:
C#
using System;
public class Outer_class {
public int number = 2000000;
public Inner_class Inner { get ; set ; }
public Outer_class() {
this .Inner = new Inner_class( this );
}
public class Inner_class {
private Outer_class obj;
public Inner_class(Outer_class outer)
{
obj = outer;
}
public void method1()
{
Console.WriteLine(obj.number);
}
}
}
public class GFG {
public static void Main()
{
Outer_class Outer = new Outer_class();
Outer.Inner.method1();
}
}
|
- 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#
using System;
public class Outer_class {
public void method1()
{
Console.WriteLine( "Outer class method" );
}
public class Inner_class {
}
}
public class Exclass : Outer_class {
public void func()
{
Console.WriteLine( "Method of derived class" );
}
}
public class GFG {
static public void Main()
{
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;
public class NestedClass
{
public void PrintOuterVariable(OuterClass outer)
{
Console.WriteLine( "The value of outerVariable is: " + outer.outerVariable);
}
}
}
OuterClass outerObj = new OuterClass();
OuterClass.NestedClass nestedObj = new OuterClass.NestedClass();
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).
Similar Reads
Partial Classes in C#
A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword. This keywo
3 min read
C# List Class
In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Thread Class
In C#, multi-threading is implemented using the Thread class, which is part of the System.Threading namespace. This class allows for the creation, management, and control of threads within an application. The Thread class provides various methods and properties to handle thread execution, prioritize
7 min read
C# Sealed Class
Sealed classes are used to restrict the users from inheriting the class. A class can be sealed by using the sealed keyword. The keyword tells the compiler that the class is sealed, and therefore, cannot be extended. No class can be derived from a sealed class. Syntax for using sealed class is attach
4 min read
C# Tuple Class
In C#, the Tuple class is used to provide static methods for creating tuples and this class is defined under the System namespace. This class itself does not represent a tuple, but it provides static methods that are used to create an instance of the tuple type. In other words, the Tuple class provi
3 min read
C# Stack Class
In C#, the Stack<T> class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it. The capa
5 min read
C# | Object Class
The Object class is the base class for all the classes in the .Net Framework. It is present in the System namespace. In C#, the .NET Base Class Library(BCL) has a language-specific alias which is Object class with the fully qualified name as System.Object. Every class in C# is directly or indirectly
4 min read
C# | Static Class
In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members and static methods. It is not allowed to create objects of the static class and since it does not allow to create objects it means it does not allow instance constructor. Stat
4 min read
Multicast Delegates in C#
A type-safe function pointer is a delegate. It means that the delegate contains a reference to a method or function, and that when we invoke the delegate, the method to which it refers will be executed. The delegate signature and the method to which it points must both be signed. The method must be
3 min read
Extension Method in C#
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is
5 min read