Introduction
An article on how to make a class sealed without using Sealed keyword. The included project includes two ways to make a class sealed using Singleton Pattern and Without singleton pattern.
The class SealedClassSingleton.cs follows a singleton pattern and instance of this class allows to invoke methods of this class.
Another workaround is not to follow singleton pattern and have all static methods and methods can be invoked by the classname as defined in the class SealedClass.cs.
The purpose of above two classes is to make them sealed without using Sealed keyword.
Using the Code
The console application contains an entry point class named clsMain.cs and the Main method in it contains two code blocks to invoke methods of two different sealed classes.
The following code defined in class DerivedClass generates compile time errors when we try to inherit from SealedClassSingleton or SealedClass classes.
<code>
public class DerivedClass:SealedClassSingleton //This class is commented as it gives compilation error
{
public DerivedClass()
{
//
// TODO: Add constructor logic here
//
}
}
</code>