Algorithm


In C#, a nested class is a class declared within another class. The outer class is often referred to as the containing or parent class, while the inner class is known as the nested class. Nested classes can be useful for organizing code, encapsulating functionality, and controlling access to certain members.

 

Code Examples

#1 C#`Program Nested Class

Code - C# Programming

using System;
namespace CsharpNestedClass {
 
  // outer class
  public class Car {

    public void displayCar() {
      Console.WriteLine("Car: Bugatti");
    }
 
    // inner class
    public class Engine {
      public void displayEngine() {
        Console.WriteLine("Engine: Petrol Engine");
      }
    }
  }
  class Program {
    static void Main(string[] args) {

      // create object of outer class
      Car sportsCar = new Car();

      // access method of outer class
      sportsCar.displayCar();
 
      // create object of inner class
      Car.Engine petrolEngine = new Car.Engine();
      
      // access member of inner class
      petrolEngine.displayEngine();
 
      Console.ReadLine();
 
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Car: Bugatti
Engine: Petrol Engine

#2 Access Outer Class Members Inside Inner Class in C#

Code - C# Programming

using System;
namespace CsharpNestedClass {

  // outer class
  public class Car {
 
    public string brand = "Bugatti";

    // nested  class
    public class Engine {
      public void displayCar() {

        // object of outer class
        Car sportsCar = new Car();
        Console.WriteLine("Brand: " + sportsCar.brand);
      }
    }
  }

  class Program {
    static void Main(string[] args) {

       // object of inner class
       Car.Engine engineObj = new Car.Engine();
       engineObj.displayCar();

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Brand: Bugatti

#3 Access static Members of Outer Class Inside Inner Class in C#

Code - C# Programming

using System;
namespace CsharpNestedClass {

  // outer class
  public class Car {
    //static member of outer class
    public static string brand = "Bugatti";

    // nested class
    public class Engine {
      public void display() {
        
        // access static member of outer class
        Console.WriteLine("Brand: " + Car.brand);
      }
    }
  }
  class Program {
    static void Main(string[] args) {

      // object of inner class
       Car.Engine obj = new Car.Engine();
       obj.display();

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Brand: Bugatti

#4 Inheriting Outer Class in C#

Code - C# Programming

using System;
namespace CsharpNestedClass {
 
  // outer class
  class Computer {

    public void display() {
      Console.WriteLine("Method of Computer class");
    }
 
    // nested class
    public class CPU {
 
    }
   }

    class Laptop : Computer {
 
    }

  class Program  {
    static void Main(string[] args) {
 
      // object of derived class
      Laptop obj = new Laptop();
      obj.display();     
 
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Method of Computer class

#5 Inheriting inner class in C#

Code - C# Programming

using System;
namespace CsharpNestedClass {

  // outer class
  class Computer {

    // nested  class
    public class CPU {
      public void display() {
        Console.WriteLine("Method of CPU class");
      }

    }
  }
    
  // inheriting inner class
  class Laptop : Computer.CPU {

  }

  class Program  {
    static void Main(string[] args) {

      // object of derived class
      Laptop obj = new Laptop();
      obj.display();     

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Method of CPU class
Advertisements

Demonstration


C# Programming Nested Class