Type.GetNestedTypes() Method in C#


The Type.GetNestedTypes() method in C# is used to get the types nested within the current Type.

Syntax

Following is the syntax −

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

Example

Let us now see an example to implement the Type.GetNestedTypes() method −

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 {
      //
   }
}

Output

This will produce the following output −

Nested Types...
Subject+BasicSubject
Subject+AdvSubject

Example

Let us now see another example to implement the Type.GetNestedTypes() method −

using System;
using System.Reflection;
public class Demo {
   public static void Main(){
      Type type1 = typeof(Subject);
      try {
         Type[] type2 = type1.GetNestedTypes(BindingFlags.Public);
         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 {
      //
   }
}

Output

This will produce the following output −

Nested Types...
Subject+BasicSubject
Subject+AdvSubject

Updated on: 11-Nov-2019

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements