Home »
.Net »
C# Programs
C# - Check a Specified Type is Nested or Not?
Learn, how to check a specified type is nested or not in C#?
Submitted by Nidhi, on October 28, 2020 [Last updated : March 22, 2023]
Checking a type is nested or not
Here, we will check a specified type is nested or not using the IsNested property of Type class.
C# program to check a specified type is nested or not
The source code to check a specified type is nested or not is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to check a specified type
//is nested or not.
using System;
using System.Reflection;
struct Struct1
{
public struct Struct2
{
void Print()
{
Console.WriteLine("Print() method called");
}
}
}
class Program
{
static void Main()
{
Type type = typeof(Struct1.Struct2);
if (type.IsNested== true)
{
Console.WriteLine("Struct2 is a nested structure");
}
else
{
Console.WriteLine("Struct2 is not a nested structure");
}
}
}
Output
Struct2 is a nested structure
Press any key to continue . . .
Explanation
In the above program, we created a structure Struct1 that contains a nested structure Struct2, and we also created a class Program. The Nested structure Struct2 contains an instance method Print().
The Program class is a non-abstract class that contains the Main() method. The Main() method is the entry point for the program. Here, we check the specified type is nested or not using the IsNested property of Type class and printed the appropriate message on the console screen.
C# Basic Programs »