Type.GetNestedTypes() Method in C#

The Type.GetNestedTypes() method in C# is used to get the types nested within the current Type. This method is particularly useful when working with reflection to discover and work with nested classes, interfaces, or other types defined within a class.

Syntax

Following is the syntax for the GetNestedTypes() method −

public Type[] GetNestedTypes();
public abstract Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr);

Parameters

The overloaded version accepts a BindingFlags parameter that specifies which nested types to return −

  • bindingAttr − A combination of BindingFlags values that control which nested types are returned (e.g., Public, NonPublic, Static, Instance).

Return Value

Returns an array of Type objects representing the nested types, or an empty array if no nested types are found.

Using GetNestedTypes() Without Parameters

The parameterless version returns all public nested types −

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 nested class
    }
    
    public class AdvSubject {
        // Another public nested class
    }
    
    private class PrivateSubject {
        // Private nested class - won't appear without BindingFlags
    }
}

The output of the above code is −

Nested Types...
Subject+BasicSubject 
Subject+AdvSubject 

Using GetNestedTypes() With BindingFlags

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

using System;
using System.Reflection;

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

public class Subject {
    public class BasicSubject {
        // Public nested class
    }
    
    public class AdvSubject {
        // Another public nested class  
    }
    
    private class PrivateSubject {
        // Private nested class
    }
    
    internal class InternalSubject {
        // Internal nested class
    }
}

The output of the above code is −

All Nested Types...
Subject+BasicSubject 
Subject+AdvSubject 
Subject+PrivateSubject 
Subject+InternalSubject 

Public Nested Types...
Subject+BasicSubject 
Subject+AdvSubject 

Common BindingFlags Values

BindingFlags Description
Public Returns only public nested types
NonPublic Returns only non-public nested types (private, protected, internal)
Public | NonPublic Returns all nested types regardless of accessibility

Conclusion

The Type.GetNestedTypes() method provides a way to discover nested types within a class using reflection. Use the parameterless version for public nested types only, or specify BindingFlags to control which nested types are returned based on their accessibility modifiers.

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

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements