Get the types nested within the current Type C#

To get the types nested within the current Type in C#, you can use the GetNestedTypes() method. This method returns an array of Type objects representing all nested types (classes, interfaces, structs, etc.) defined within a type.

Syntax

Following is the syntax for getting nested types −

Type[] nestedTypes = type.GetNestedTypes();

To specify visibility, use the overload with BindingFlags

Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic);

Using GetNestedTypes() Method

The GetNestedTypes() method retrieves all public nested types by default. Here's a basic example −

Example

using System;

public class Demo {
   public static void Main() {
      Type type1 = typeof(Subject);
      try {
         Type[] type2 = type1.GetNestedTypes();
         Console.WriteLine("Nested Types...");
         for (int i = 0; i < type2.Length; i++)
            Console.WriteLine("{0} ", type2[i]);
      }
      catch (ArgumentNullException e) {
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}

public class Subject {
   public class BasicSubject {
      //
   }
   public class AdvSubject {
      //
   }
}

The output of the above code is −

Nested Types...
Subject+BasicSubject 
Subject+AdvSubject 

Using GetNestedTypes() with BindingFlags

You can specify visibility constraints using BindingFlags to control which nested types are returned −

Example

using System;
using System.Reflection;

public class Demo {
   public static void Main() {
      Type type1 = typeof(Subject);
      try {
         Type[] publicTypes = type1.GetNestedTypes(BindingFlags.Public);
         Type[] allTypes = type1.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic);
         
         Console.WriteLine("Public Nested Types...");
         for (int i = 0; i < publicTypes.Length; i++)
            Console.WriteLine("{0} ", publicTypes[i]);
            
         Console.WriteLine("\nAll Nested Types...");
         for (int i = 0; i < allTypes.Length; i++)
            Console.WriteLine("{0} ", allTypes[i]);
      }
      catch (ArgumentNullException e) {
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}

public class Subject {
   public class BasicSubject {
      //
   }
   private class PrivateSubject {
      //
   }
   protected class ProtectedSubject {
      //
   }
}

The output of the above code is −

Public Nested Types...
Subject+BasicSubject 

All Nested Types...
Subject+BasicSubject 
Subject+PrivateSubject 
Subject+ProtectedSubject 

Common BindingFlags Values

BindingFlag Description
Public Returns only public nested types
NonPublic Returns only non-public nested types (private, protected, internal)
Instance Returns instance nested types
Static Returns static nested types

Conclusion

The GetNestedTypes() method provides an easy way to discover all types nested within a class using reflection. Use BindingFlags to control visibility and access modifiers when retrieving nested types programmatically.

Updated on: 2026-03-17T07:04:36+05:30

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements