C# Program to Check a Specified Class is a Sealed Class or not
Last Updated :
09 Dec, 2021
A Sealed class is a class that will not let the users inherit the class. We can create a sealed class using sealed keywords. This keyword tells the compiler that the class is a sealed class. In this article, we will learn how to check the specified class is a sealed class or not. So we use the IsSealed property of the Type class. This property is used to check whether the given Type is sealed or not.
Syntax:
public bool IsSealed { get; }
Return: The return type of this property is boolean. It will return true if the given Type or class is sealed, otherwise, it will return false.
Example 1:
C#
using System;
using System.Reflection;
public class Myclass1
{
public void display()
{
Console.WriteLine( "Hello! GeeksforGeeks" );
}
}
sealed class Myclass2
{
public void Show()
{
Console.WriteLine( "Hey! GeeksforGeeks" );
}
}
class GFG{
public static void Main( string [] args)
{
Console.WriteLine( typeof (Myclass1).IsSealed);
Console.WriteLine( typeof (Myclass2).IsSealed);
}
}
|
Output:
False
True
Example 2:
C#
using System;
using System.Reflection;
sealed class Myclass
{
public void Show()
{
Console.WriteLine( "Hey! GeeksforGeeks" );
}
}
class GFG{
public static void Main( string [] args)
{
if ( typeof (Myclass).IsSealed == true )
{
Console.WriteLine( "The given class is a sealed class" );
}
else
{
Console.WriteLine( "The given class is not a sealed class" );
}
}
}
|
Output:
The given class is a sealed class
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...