Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding C# Nested Classes: Definition, Usage, and Inheritance

Understanding C# Nested Classes

Explore how to define, access, and inherit nested classes in C# with practical examples that demonstrate best practices.

In C#, a class can be declared inside another class. The inner type is referred to as a nested class. For example:

class OuterClass
{
    // ... outer members

    class InnerClass
    {
        // ... inner members
    }
}

This example creates InnerClass inside OuterClass. The inner type is only visible within the outer type unless its access modifier allows otherwise.


Accessing Nested Class Members

To work with nested types, you first instantiate each class separately.

1. Create an instance of the outer class

OuterClass outer = new OuterClass();

Here, outer is an object of OuterClass.

2. Create an instance of the nested class

OuterClass.InnerClass inner = new OuterClass.InnerClass();

Because InnerClass is nested inside OuterClass, you qualify it with the outer type name.

Once instantiated, you can call members through the object reference.

Example: Nested Class with Methods

using System;
namespace NestedClassDemo
{
    public class Car
    {
        public void DisplayCar() => Console.WriteLine("Car: Bugatti");

        public class Engine
        {
            public void DisplayEngine() => Console.WriteLine("Engine: Petrol Engine");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car sportsCar = new Car();
            sportsCar.DisplayCar();

            Car.Engine petrolEngine = new Car.Engine();
            petrolEngine.DisplayEngine();

            Console.ReadLine();
        }
    }
}

Output

Car: Bugatti
Engine: Petrol Engine

The example shows how to create objects of both the outer Car and the nested Engine and invoke their methods.

Note that you cannot call a nested type’s method directly from the outer instance:

// Invalid
sportsCar.DisplayEngine();

Accessing Outer Class Members from Inside a Nested Class

Nested classes can reference members of their containing type. You can use an instance of the outer type or the outer type name for static members.

Using an Instance to Access Instance Members

using System;
namespace NestedClassDemo
{
    public class Car
    {
        public string Brand = "Bugatti";

        public class Engine
        {
            public void ShowBrand()
            {
                Car car = new Car();
                Console.WriteLine("Brand: " + car.Brand);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car.Engine engine = new Car.Engine();
            engine.ShowBrand();
            Console.ReadLine();
        }
    }
}

Output

Brand: Bugatti

Accessing Static Members Without Instantiation

using System;
namespace NestedClassDemo
{
    public class Car
    {
        public static string Brand = "Bugatti";

        public class Engine
        {
            public void ShowBrand()
            {
                Console.WriteLine("Brand: " + Car.Brand);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car.Engine engine = new Car.Engine();
            engine.ShowBrand();
            Console.ReadLine();
        }
    }
}

Output

Brand: Bugatti

Inheritance Involving Nested Classes

Nested classes can participate in inheritance just like any other type.

Deriving a Class from an Outer Class

using System;
namespace NestedClassDemo
{
    class Computer
    {
        public void Display() => Console.WriteLine("Method of Computer class");

        public class CPU
        {
            // Additional members can be added here
        }
    }

    class Laptop : Computer
    {
        // Laptop inherits all members of Computer
    }

    class Program
    {
        static void Main(string[] args)
        {
            Laptop laptop = new Laptop();
            laptop.Display();
            Console.ReadLine();
        }
    }
}

Output

Method of Computer class

Deriving a Class from a Nested Class

using System;
namespace NestedClassDemo
{
    class Computer
    {
        public class CPU
        {
            public void Display() => Console.WriteLine("Method of CPU class");
        }
    }

    class Laptop : Computer.CPU
    {
        // Laptop inherits members of Computer.CPU
    }

    class Program
    {
        static void Main(string[] args)
        {
            Laptop laptop = new Laptop();
            laptop.Display();
            Console.ReadLine();
        }
    }
}

Output

Method of CPU class

When inheriting a nested class, you must qualify the type with its outer class name, e.g., Computer.CPU.

C Language

  1. C# Constructors: Types, Examples, and Advanced Patterns
  2. C# Static Keyword: Mastering Static Variables, Methods, and Classes
  3. Mastering C# Inheritance: Concepts, Types, and Practical Code
  4. Understanding C# Nested Classes: Definition, Usage, and Inheritance
  5. Mastering Constructor Overloading in C#
  6. Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
  7. Understanding Java Nested Static Classes: Usage, Differences, and Examples
  8. Streamlining Maintenance with Fiix's Nested PM: One Schedule, Multiple Tasks
  9. Mastering C# Inheritance: Build Reusable, Maintainable Code
  10. C# Polymorphism: Static vs Dynamic Binding Explained