Alex Rivera | Logout

Why declare static classes as sealed and abstract in C#?

Asked 2009-08-12T22:02:20.677
9

This MSDN article states that static classes should be declared as sealed and abstract. I was under the impression that static classes were already sealed. Why would you also need to declare a static class as sealed?

c#
Edit
Report

2 Answers

27

I think the pertinent bit from that article is:

"Do declare static classes as sealed and abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes."

Remember, the .NET framework supports a lot of different languages.

answered 2009-08-12T22:05:42.527
18

C# v1 did not allow 'static' keyword on class. So if you have a class with only static methods, it was suggested to declare it 'sealed', 'abstract' and make constructor private. This means that no one can instantiate your class or try to inherit from it. [It doesn't make sense to inherit from a class which has only static methods.]

C# v2 allowed the static keyword on a class so you don't have to use the above trick to enforce correct usage.

answered 2009-08-12T22:11:32.220

Your Answer