c sharp

IsSealed Property

In some cases in the Object Oriented Programming through C#, we will not let the child classes inherit the methods, variables, etc. from the parent class. In order to do this, we have to make the class as sealed. By specifying the sealed keyword in front of the class, we can make the type of the class as sealed.

Let’s look into it.

Structure:

sealed public class Class_name
{
         //we can declare methods
        //we can declare variables
}

IsSealed Property

The IsSealed property from the Type class checks if the specified Type is sealed or not. If it is sealed, it returns True. Otherwise, it returns False if it is not a sealed class.

Syntax:

typeof(Type).IsSealed

Return Type:

It returns the Boolean value (True/False).

Example 1:

Let’s create a C# Application with a class named Linuxhint and check if it is sealed or not.

using System;
class Linuxhint
{
    static public  void Main(){
       
    //check the Linuxhint is sealed or not
Console.WriteLine("Is Linuxhint Sealed or not?: "+ typeof(Linuxhint).IsSealed);
 
}
}

Output:

Explanation:

Line 7:
Check if the Linuxhint class is sealed or not

Since it is not sealed, False is returned.

Example 2:

Let’s create the three sealed classes named Chocos, Chips, and Veg. Check if they are sealed or not.

using System;


sealed public class Chocos{
   //this is Chocos sealed class
}

sealed public class Chips{
    //this is Chips sealed class
}

sealed public class Veg{
    //this is Veg sealed class
}

class Linuxhint
{
    static public  void Main(){
       
    //check the Chocos is sealed or not
Console.WriteLine("Is Chocos Sealed ?: "+ typeof(Chocos).IsSealed);
    //check the Chips is sealed or not
Console.WriteLine("Is Chips Sealed ?: "+ typeof(Chips).IsSealed);  
    //check the Veg is sealed or not
Console.WriteLine("Is Veg Sealed ?: "+ typeof(Veg).IsSealed);
}
}

Output:

Explanation:

Line 4-14:

Create three sealed classes.

Line 20-25:

Check if the above classes are sealed or not.

Since they are sealed, True is returned.

Example 3:

Let’s create the three sealed classes named Chocos, Chips, and Veg inside the Linuxhint class. Check if they are sealed or not.

using System;


class Linuxhint
{
     sealed public class Chocos{
   //this is Chocos sealed class
}

sealed public class Chips{
    //this is Chips sealed class
}

sealed public class Veg{
    //this is Veg sealed class
}
    static public  void Main(){
   

    //check the Chocos is sealed or not
Console.WriteLine("Is Chocos Sealed ?: "+ typeof(Chocos).IsSealed);
    //check the Chips is sealed or not
Console.WriteLine("Is Chips Sealed ?: "+ typeof(Chips).IsSealed);  
    //check the Veg is sealed or not
Console.WriteLine("Is Veg Sealed ?: "+ typeof(Veg).IsSealed);
}
}

Output:

Explanation:

Line 6-16:

Create the three sealed classes inside the Linuxhint class.

Line 20-25:

Check if the previous classes are sealed or not.

Since they are sealed, True is returned.

Conclusion

In this C# tutorial, we learned how to check if the type is sealed or not using the IsSealed property. This property tells us by returning a Boolean value with three examples. If it is True, we can say that the type of the class is a sealed class. If it is False, we can say that the type is not a sealed class.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain