Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Nested types in C#

Reference for C# nested types: classes, structs, enums and interfaces declared inside another type, accessibility rules, access to the outer instance and when to use them.

A nested type is a type declared inside another class, struct, record or interface. You use it for helpers that belong to one type only: a node class inside a linked list, an enumerator inside a collection, a builder inside the object it builds. Nested types can be classes, structs, records, interfaces, enums and delegates.

Because a nested type is a member of the outer type, it can have any accessibility, including private, which no top-level type can have. It can also see the outer type's private members, but it does not have an automatic reference to an outer instance, unlike Java's inner classes.

C#
class LinkedList
{
    private class Node { public int Value; public Node? Next; }   // hidden helper
    private Node? head;
}

Try it

C# A private helper and public nested types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Reaching outer members

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
class Outer
{
    public class A { }
    private struct B { }
    protected enum C { X }
    internal interface IChild { }
    private protected record D(int Id);
}
// Outside: new Outer.A();
Aspect Rule
Accessibility Any of the six modifiers. Default is private. A nested type can be public inside an internal outer type, but effective visibility is the smaller of the two.
Name Referenced as Outer.Inner (or by simple name inside Outer).
Outer members A nested type can access all members of the enclosing type, including private ones, through an instance. It can use static members by their simple name.
Inner instance No implicit outer instance exists; pass one in explicitly.
Generic outer A nested type of Outer<T> is itself generic over T: Outer<int>.Inner and Outer<string>.Inner are different types.

Errors: using a private nested type from outside gives CS0122 ("is inaccessible due to its protection level"); a nested type with the same name as its enclosing type gives CS0542 ("member names cannot be the same as their enclosing type").

Usage notes

  • Use for implementation details. Private nested types keep helpers out of the namespace and out of the public API. Public nested types are rare outside enums and small related records.
  • Static classes can nest types, and nested static classes can group constants; but do not use classes just as namespaces.
  • Outer instance is explicit. C# has no inner-class capture; the Step class above stores owner itself. That avoids hidden memory leaks.
  • Inheritance. A nested class can derive from its outer class, which is how some factory patterns hide implementations: abstract class Shape { private class Circle : Shape { } public static Shape Create() => new Circle(); }.
  • Reflection. Nested type names carry a +: typeof(Counter.Step).FullName is Counter+Step.
  • Testing. Private nested types are not directly reachable from tests; test through the outer type's behavior.
  • Prefer a top-level type when two other types need it or when its code grows large; file-scoped (file) types are another way to hide a helper within one source file.