Javatpoint Logo
Javatpoint Logo

C# Program to Check a Specified Class is a Sealed Class or not

C is a flexible programming language commonly used for developing applications in the Microsoft universe. One of the interesting features of C# is defining classes as sealed, limiting the inheritance of a class.

In this post, we will explore the idea of sealed classes and offer a C# program that enables you to determine whether a given class is sealed or not.

Understanding Sealed Classes:

A sealed class in C# is a class that cannot be inherited. Once a class is sealed, it does not allow the other classes to obtain from it. It can be helpful when you need to limit the application of a specific class to ensure specific behavior or design.

Syntax for Defining a Sealed Class:

The sealed keyword is used for declaring a sealed class in C#. Here's the basic syntax:

In the above case, the SealedClass is marked sealed, meaning that no other class can inherit it.

C# Program to Check if a Class is Sealed:

Next, let's take a look at the C# program that determines whether a particular class is sealed or not. This program makes use of reflection, a strong characteristic of C# that can be used to investigate and interoperate with the type metadata at runtime.

Output:

SampleClass is a sealed class.
  • IsClassSealed Method:

This approach is meant to find out whether a given class is sealed or not. It accepts a Type parameter, which is the type information of a class. It uses the IsSealed property of the Type class to determine if the class is sealed.

  • Main Method:

The Main method is the starting point of the program. It starts by defining the class to be checked with the variable classNameToCheck.

  • Reflection for Obtaining Type:

The reflection technique is utilized by Type. GetType(classNameToCheck) to return the Type object of the given class. Reflection is an aspect of C#, which allows introspection and manipulation of the metadata of types at runtime.

  • Checking if the Class is Sealed:

Finally, the acquired Type object is passed to the IsClassSealed method to check if the class is sealed.
The outcome of this test is a boolean, whether the class is sealed or not.

  • Displaying the Result:

Based on the results of the check, the program provides an output message whether the specified class is sealed or not sealed.

If the class is closed, it prints a message to that effect. Otherwise, it prints a message that the class is not sealed.

Conclusion:

In summary, the provided C# program is a useful resource for developers who want to know if a given class is sealed, thereby restricting inheritance. The use of reflection emphasizes the adaptability of the language, enabling runtime analysis of type metadata. Through the IsSealed property, the IsClassSealed method takes advantage by providing an easy and quick way of determining if a class is sealed.

It serves an essential purpose in C # design, since sealed classes represent a way of limiting the extensibility of classes for the developers. Sealed classes also protect against inheritance and ensure a level of encapsulation while also helping to create strong and secure codebases.

The practicality of the program is further heightened by its user-friendly configuration where developers need only mention the name of the class of interest. The output messages clearly summarize if the queried class is sealed or not, which saves time for making a decision in the development process.

More generally, this program reflects the beauty and eloquence of C# as a programming language. The balance of characteristics of reflection and sealed classes highlights the language's focus on giving developers substantial and sensible instruments for creating durable and secure software products.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news