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#
using System;
using System.Reflection;
struct Geeks
{
public struct Gfg2
{
void hello()
{
Console.WriteLine( "hello geeks!" );
}
}
}
class GFG{
static void Main()
{
Console.WriteLine( typeof (Geeks.Gfg2).IsNested);
}
}
|
Output:
True
Example 2:
C#
using System;
using System.Reflection;
public class Geeks
{
public class Gfg2
{
void myfun()
{
Console.WriteLine( "hello geeks!" );
}
}
}
class GFG{
static void Main()
{
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
Last Updated :
23 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...