locked
When to use a sealed class and its benefits RRS feed

  • Question

  • User1884398186 posted

    Is it a good idea to use sealed classes, and is there any benefit like performance  associated with sealed classes as compared to normal class?

    I know that a sealed class cannot be inherited, but why should I use it instead of a normal class?

    Wednesday, September 3, 2014 11:30 PM

Answers

  • User-821857111 posted
    If there are performance gains they are likely to be insignificant. No one would implement the sealed keyword for performance reasons. You would only use it to prevent others from deriving from your class.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 4, 2014 1:47 AM

All replies

  • User-821857111 posted
    If there are performance gains they are likely to be insignificant. No one would implement the sealed keyword for performance reasons. You would only use it to prevent others from deriving from your class.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 4, 2014 1:47 AM
  • User-646145796 posted

    Hi,

    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, see more at: http://www.codeproject.com/Articles/239939/Csharp-Tweaks-Why-to-use-the-sealed-keyword-on-cla

    Best Regards,

    Jambor

    Friday, September 5, 2014 4:19 AM
  • User-821857111 posted

    Using sealed keyword brings not only performance win

    The article you referenced doesn't specify what kind of performance "win" there is to be had and only refers vaguely to something half-remembered by the author. One should be careful when quoting selectively from articles like that. The article completely fails to demonstrate that there is a performance gain of any significance.

    Friday, September 5, 2014 5:18 AM
  • User1884398186 posted

    I did some research and found a mention about performance gain with sealed classes, but its not explained in detail.  I came across this on MSDN at  http://msdn.microsoft.com/en-us/library/ms173150.aspx

    The documentation given in above URL said the following about sealed classes.

    "A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster."

    Friday, September 5, 2014 11:28 AM
  • User1052662409 posted

    you should read this also

    http://forums.asp.net/t/1819044.aspx?Static+class+vs+Sealed+class

    Monday, October 20, 2014 12:39 AM