Click here to Skip to main content
15,917,176 members
Articles / Programming Languages / C#
Article

How to make a class Sealed without Sealed Keyword

Rate me:
Please Sign up or sign in to vote.
1.46/5 (9 votes)
10 Aug 2006 24.7K   214   9   5
We can make a class sealed without sealed keyword

Sample Image - Class_Without_Sealed.jpg

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>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Biswa have been working on Microsoft Technologies since 7 years. For last 3 years he is working on C# as Consultant in Telecommunications sector.

Comments and Discussions

 
QuestionSealed class without using sealed keyword Pin
VikasAgarwal8416-Oct-11 23:05
VikasAgarwal8416-Oct-11 23:05 
GeneralMy vote of 1 Pin
Michael B. Hansen10-May-09 20:22
Michael B. Hansen10-May-09 20:22 
It's just an implementation of the singleton pattern. And it could leave people to miss the benefits of using the sealed keyword.
Questionwhy? Pin
JanWilmans3-Jul-07 1:03
JanWilmans3-Jul-07 1:03 
AnswerThe benefits Pin
mwanyu27-Sep-07 1:42
mwanyu27-Sep-07 1:42 
GeneralRe: The benefits Pin
screanjap27-Sep-07 2:29
screanjap27-Sep-07 2:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.