Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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#




// C# program to check if the given 
// class is sealed or not
using System;
using System.Reflection;
  
// Declare a class without sealed
public class Myclass1
{
    public void display()
    {
        Console.WriteLine("Hello! GeeksforGeeks");
    }
}
  
// Declare a class with sealed
sealed class Myclass2 
{
    public void Show()
    {
        Console.WriteLine("Hey! GeeksforGeeks");
    }
}
  
// Driver code
class GFG{
  
public static void Main(string[] args)
{
      
    // Check the given class is sealed or not
    // Using IsSealed property
    Console.WriteLine(typeof(Myclass1).IsSealed);
    Console.WriteLine(typeof(Myclass2).IsSealed);
}
}

Output:

False
True

Example 2:

C#




// C# program to check if the given 
// class is sealed or not
using System;
using System.Reflection;
  
// Declare a class with sealed keyword
sealed class Myclass 
{
    public void Show()
    {
        Console.WriteLine("Hey! GeeksforGeeks");
    }
}
  
// Driver code
class GFG{
  
public static void Main(string[] args)
{
      
    // Check the given class is sealed or not
    // Using IsSealed property
    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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!