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