C# Program to Check a Specified Type is Nested or not
In programming languages, nested means a class or a method or loop or structure is present inside another class or a method or loop or structure. In C#, we can check a specified type is nested or not using the IsNested property of the Type class. This property returns a value that represents whether the specified type(i.e., class, struct, etc) definition is nested inside another type definition. It will return true if the specified type is nested, otherwise returns false.
Syntax:
public bool IsNested { get; }
Example 1:
C#
// C# program to check a specified // type is nested or not using System; using System.Reflection; // Create a structure struct Geeks { // Create a nested structure named Gfg2 // with hello() method public struct Gfg2 { void hello() { Console.WriteLine( "hello geeks!" ); } } } class GFG{ // Driver code static void Main() { // Check the type is nested or not Console.WriteLine( typeof (Geeks.Gfg2).IsNested); } } |
Output:
True
Example 2:
C#
// C# program to check a specified // type is nested or not using System; using System.Reflection; // Create a class public class Geeks { // Create a nested class // with hello() method public class Gfg2 { void myfun() { Console.WriteLine( "hello geeks!" ); } } } class GFG{ // Driver code static void Main() { // Check the type is nested or not if ( typeof (Geeks.Gfg2).IsNested == true ) { Console.WriteLine( "Gfg2 class is nested class" ); } else { Console.WriteLine( "Gfg2 class is not nested class" ); } } } |
Output:
Gfg2 class is nested class
Please Login to comment...