Click here to Skip to main content
13,344,281 members (52,908 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 12 Aug 2011

C# Tweaks – Why To Use the sealed Keyword on Classes?

, 12 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Why to use the sealed keyword on classes?

The sealed keyword is one of the very seldom used modifiers in C#. Probably most of you know what it is for, but many developers have not ever used it.

See C# Programmers Guide if you are not sure you remember what the keyword is good for: sealed keyword (MSDN).

Why Shall I Use It?

The most popular, but not really most important motivation is the performance – JIT compiler can produce more efficient code by calling the method non-virtually. I remember someone even made performance measurements, but I think that the real performance gain highly depends on algorithms in a specific use case.

The vast majority of .NET programmers let their classes “unsealed” without even considering making them sealed. If a class was not designedly made inheritable, it is very probably even impossible to inherit from it and override members reasonably. On the other hand, overriding members of the class which were not designed to be overridden might cause unpredictable results.

When a class is originally sealed, it can change to “unsealed” in the future without breaking compatibility.

Something New I’ve Discovered Recently

Recently, I was refactoring some component with multiple classes making intensive use of inheritance. During cleanup, I changed all leaf classes in inheritance tree, the classes which cannot be inherited anymore, to be sealed. I was sure it will not break compatibility, but the next compile failed.

The reason was a bug, which became visible only after I made some class sealed to compile time. Not being sealed, it would throw an exception during execution. This sample demonstrates the simplified version of this situation:

interface IInterface1 {}

class Class1 {}

class Program
   static void Main(string[] args)
     //Class1 does not implement IInterface1
     Class1 instanceOfClass1 = new Class1();

     //However this cast does not leads to compilation error
     IInterface1 someImplementer = (IInterface1)instanceOfClass1;

Class1 does not implement IInterface1, however the cast of an instance of the Class1 to IInterface1 does not lead to compilation error. The reason is that theoretically some inherited class of the Class1 might implement this interface.

Now let’s make Class1 sealed. Now the compiler will see that Class1 can be only Class1 “itself” (and its base classes if applicable) and it does not implement interface IInterface1.

internal interface IInterface1 {}

sealed class Class1 {}

class Program
    static void Main(string[] args)
        //Class1 does not implement IInterface1
        Class1 instanceOfClass1 = new Class1();

        //However this cast does not leads to compilation error
        IInterface1 someImplementer = (IInterface1)instanceOfClass1;

The following compilation error will occur:

Cannot convert type 'Class1' to 'IInterface1'

So using sealed keyword brings not only performance win and additional code access security but also helps to write bug free code, making better use of the .NET type safety.

My recommendation: Always declare a new class as sealed until you are writing an abstract class or a class which must be inherited per design. Most classes in a real application (except you are writing a widely used library) can be made sealed.

P.S. You can apply the sealed keyword not only to classes but also to some members. I am going to post about that as well.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

George Mamaladze
Software Developer
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze

You may also be interested in...


Comments and Discussions

Question[My vote of 2] Not good Pin
Maxx Velocity22-Feb-17 10:54
memberMaxx Velocity22-Feb-17 10:54 
GeneralMy vote of 5 Pin
Rohit Sardana27-Sep-16 19:47
professionalRohit Sardana27-Sep-16 19:47 
GeneralMy vote of 5 Pin
Mohammad Khalid117-May-16 21:49
memberMohammad Khalid117-May-16 21:49 
AnswerNot a good idea Pin
TobiasLawrenz27-Jan-15 3:45
memberTobiasLawrenz27-Jan-15 3:45 
QuestionMy Vote of 5 Pin
ashok rathod23-Jul-14 21:35
professionalashok rathod23-Jul-14 21:35 
GeneralNice Article Pin
Amey K Bhatkar25-Mar-14 20:18
professionalAmey K Bhatkar25-Mar-14 20:18 
GeneralMy vote of 5 Pin
Tarek Elqusi4-Mar-13 23:01
memberTarek Elqusi4-Mar-13 23:01 
GeneralMy vote of 5 Pin
nikhil _singh27-Jul-12 2:59
membernikhil _singh27-Jul-12 2:59 
GeneralMy vote of 1 Pin
SAKryukov3-Jun-12 22:47
mvpSAKryukov3-Jun-12 22:47 
GeneralRe: My vote of 1 Pin
Paulo Zemek6-Jun-12 8:08
mvpPaulo Zemek6-Jun-12 8:08 
GeneralRe: My vote of 1 Pin
Sergey Alexandrovich Kryukov29-Jun-12 10:09
mvpSergey Alexandrovich Kryukov29-Jun-12 10:09 
Wait, wait. Here is what I mean: mark a class sealed, create some software; measure performance; then remove sealed, measure performance. Are you saying the performance will degrade after that? If don't think so, and I don't even think that you think so. If you do think so, this is too serious; I would like to see some proof then.

Indirect effect on the developers making them to write better performing code? Why not? But performance of sealed class? Seriously?

Sergey A Kryukov

GeneralRe: My vote of 1 Pin
Paulo Zemek29-Jun-12 10:23
mvpPaulo Zemek29-Jun-12 10:23 
GeneralMy vote of 5 Pin
AhsanS18-May-12 3:22
memberAhsanS18-May-12 3:22 
GeneralMy vote of 4 Pin
akosidab14-May-12 20:24
memberakosidab14-May-12 20:24 
Questionreally useful info Pin
Rahul Rajat Singh14-Feb-12 8:03
memberRahul Rajat Singh14-Feb-12 8:03 
Question[My vote of 1] performance Pin
radioman.lt19-Oct-11 3:10
memberradioman.lt19-Oct-11 3:10 
AnswerRe: [My vote of 1] performance Pin
George Mamaladze19-Oct-11 5:51
memberGeorge Mamaladze19-Oct-11 5:51 
GeneralRe: [My vote of 1] performance Pin
SAKryukov3-Jun-12 22:55
mvpSAKryukov3-Jun-12 22:55 
AnswerRe: [My vote of 1] performance Pin
SAKryukov3-Jun-12 22:53
mvpSAKryukov3-Jun-12 22:53 
QuestionVoted 5 Pin
zenwalker198514-Oct-11 1:27
memberzenwalker198514-Oct-11 1:27 
GeneralMy vote of 5 Pin
Reiss17-Aug-11 4:30
memberReiss17-Aug-11 4:30 
QuestionGood info, small typo :) Pin
Ant210015-Aug-11 6:38
memberAnt210015-Aug-11 6:38 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 12 Aug 2011
Article Copyright 2011 by George Mamaladze
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid