Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Type.GetElementType() Method
Next article icon

C# | Type.GetNestedTypes() Method

Last Updated : 08 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Type.GetNestedTypes() Method is used to get the types nested within the current Type. There are 2 methods in the overload list of this method as follows:
 

GetNestedTypes() Method

This method is used to return the public types nested in the current Type.
 

Syntax: public Type[] GetNestedTypes ();
Return Value: This method returns an array of Type objects representing the public types nested in the current Type (the search is not recursive), or an empty array of type  if no public types are nested in the current Type. 
 

Below programs illustrate the use of the above-discussed method:
Example 1:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() Method
using System;
using System.Globalization;
using System.Reflection;
 
// Defining class Empty
public class Empty { }
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Person);
 
        // try-catch block for handling Exception
        try {
 
            // Getting the types nested by
            // using GetNestedTypes() Method
            Type[] type = objType.GetNestedTypes();
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine("{0} ", type[i]);
        }
 
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
public class Person {
 
    public class Student
    {
        // string name;
        // int roll;
        // string dept;
    }
 
    public class Teacher
    {
        // string name;
        // string dept;
        // int id;
    }
}
 
 
Output: 
NestedType of current type is:  Person+Student  Person+Teacher

 

Example 2:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() Method
using System;
using System.Globalization;
using System.Reflection;
 
// Defining class Empty
public class Empty { }
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Animal);
 
        // try-catch block for handling Exception
        try {
 
            // Getting the types nested by
            // using GetNestedTypes() Method
            Type[] type = objType.GetNestedTypes();
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
 
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
public class Animal {
 
    public class Cat {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Dog {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Mouse {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public interface Description {
 
        string getBreed();
        string getColor();
        string getType();
        bool isAlive();
    }
}
 
 
Output
NestedType of current type is:   Animal+Cat  Animal+Dog  Animal+Mouse  Animal+Description

GetNestedTypes(BindingFlags) Method

This method is used to search for the types nested in the current Type, using the specified binding constraints when overridden in a derived class.
 

Syntax: public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr); 
Here, it takes a bitmask comprised of one or more BindingFlags that specify how the search is conducted or, Zero, to return null.
Return Value: This method returns an array of Type objects representing all the types nested in the current Type that match the specified binding constraints (the search is not recursive), or an empty array of type if no nested types are found that match the binding constraints. 
 

Example 1:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() Method
using System;
using System.Globalization;
using System.Reflection;
 
// Defining class Empty
public class Empty { }
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Animal);
 
        // try-catch block for handling Exception
        try {
 
            // Getting the types nested by
            // using GetNestedTypes() Method
            Type[] type = objType.GetNestedTypes();
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
 
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
public class Animal {
 
    public class Cat {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Dog {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public class Mouse {
 
        // string breed;
        // string color;
        // string type;
    }
 
    public interface Description {
 
        string getBreed();
        string getColor();
        string getType();
        bool isAlive();
    }
}
 
 
Output: 
NestedType of current type is:   Animal+Empty

 

Example 2:
 

csharp




// C# program to demonstrate the
// Type.GetNestedTypes() Method
using System;
using System.Globalization;
using System.Reflection;
 
// Defining class Empty
public class Empty { }
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Person);
 
        // try-catch block for handling Exception
        try {
 
            // Getting the types nested by
            // using GetNestedTypes() Method
            Type[] type = objType.GetNestedTypes(BindingFlags.Public);
 
            // Display the Result
            Console.WriteLine("NestedType of current type is: ");
 
            for (int i = 0; i < type.Length; i++)
                Console.WriteLine(" {0}", type[i]);
        }
 
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("name is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
public class Person {
 
    public class Student
    {
        // string name;
        // int roll;
        // string dept;
    }
 
    public class Teacher
    {
        // string name;
        // string dept;
        // int id;
    }
}
 
 
Output: 
NestedType of current type is:   Person+Student  Person+Teacher

 

Reference:
 

  • https://docs.microsoft.com/en-us/dotnet/api/system.type.getnestedtypes?view=netframework-4.8

 



Next Article
C# | Type.GetElementType() Method

R

RohitPrasad3
Improve
Article Tags :
  • C#
  • CSharp-method
  • CSharp-Type-Class

Similar Reads

  • C# | Type.GetNestedType() Method
    Type.GetNestedType() Method is used to get a specific type nested within the current Type. There are 2 methods in the overload list of this method as follows: GetNestedType(String, BindingFlags) Method GetNestedType(String) Method GetNestedType(String, BindingFlags) Method This method is used to sea
    4 min read
  • C# | Type.GetElementType() Method
    Type.GetElementType() Method is used to return the Type of the object encompassed or referred to by the current array, pointer or reference type when overridden in a derived class. Syntax: public abstract Type GetElementType (); Return Value: This method returns the Type of the object encompassed or
    2 min read
  • C# | Type.GetMethods() Method
    Type.GetMethods() Method is used to get the methods of the current Type. There are 2 methods in the overload list of this method as follows: GetMethods(BindingFlags) Method GetMethods() Method GetMethods(BindingFlags) Method This method is used to search for the methods defined for the current Type,
    5 min read
  • C# | Type.GetInterfaces() Method
    Type.GetInterfaces() Method is used to get all the interfaces implemented or inherited by the current Type when overridden in a derived class. Syntax: public abstract Type[] GetInterfaces ();Return Value: This method returns an array of Type objects representing all the interfaces implemented or inh
    2 min read
  • C# | Type.GetTypeCode() Method
    Type.GetTypeCode() Method is used to get the underlying type code of the specified Type. Syntax: public static TypeCode GetTypeCode (Type type); Here, it takes the type whose underlying type code to get. Return Value: This method returns the code of the underlying type, or Empty if type is null. Bel
    2 min read
  • C# | Type.GetTypeArray() Method
    Type.GetTypeArray() Method is used to get the types of the objects in the specified array. Syntax: public static Type[] GetTypeArray (object[] args); Here, it takes an array of objects whose types to determine. Return Value: This method returns an array of Type objects representing the types of the
    2 min read
  • C# | Type.GetInterface() Method
    Type.GetInterface() Method is used to gets a specific interface implemented or inherited by the current Type. GetInterface(String) Method This method is used to search for the interface with the specified name. Syntax: public Type GetInterface (string name); Here, it takes the string containing the
    4 min read
  • C# | Type.GetFields() Method
    Type.GetFields() Method is used to get the fields of the current Type. There are 2 methods in the overload list of this method as follows: GetFields() Method GetFields(BindingFlags) Method GetFields() Method This method is used to return all the public fields of the current Type. Syntax: public Syst
    5 min read
  • C# | Type.GetMembers() Method
    Type.GetMembers() Method is used to get the members (properties, methods, fields, events, and so on) of the current Type. There are 2 methods in the overload list of this method as follows: GetMembers() Method GetMembers(BindingFlags) Method GetMembers() Method This method is used to return all the
    4 min read
  • C# | Type.GetTypeHandle() Method
    Type.GetTypeHandle() Method is used to get the handle for the Type of a specified object. Syntax: public static RuntimeTypeHandle GetTypeHandle (object o); Here, it takes the object for which to get the type handle. Return Value: This method returns The handle for the Type of the specified Object. E
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences