views:

71

answers:

2

Just a small question about c++/cli. Abstract classes have abstract methods to be implemented by derived classes, sealed classes dont allow inheritance.

So why we have some classes in .NET base class library defined as abstract sealed, and you can find plenty .. ??!

+6  A: 

It is equivalent to "static class" in the C# language. The language that was used to write almost all of the BCL classes. All the methods must be static. Declaring it abstract and sealed prevents anybody from deriving from the class and creating an instance of it.

The class methods are the exact equivalent of free functions in the C and C++ language. Something the CLR does not support.

Hans Passant
i totally agree with you, becuause it looks like JAVA'S MATH class for instance, but why the msdn documentation state that this isn't possible or right if u can do it ??!
Ebraheem Najjar
@Ebraheem Najjar: It's only possible to do this directly at the IL level. The compiler won't let you write a `sealed abstract` class directly.
Scott Dorman
The IL attributes on a class are distinct from the keywords in the language. In C#, it makes no sense to declare a class sealed *and* abstract, it will complain. Declaring it static is what turns these IL attributes on. There is no CLR analogue for a 'static' class.
Hans Passant
+1  A: 

They can't both abstract and sealed, it makes no sense

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.

http://msdn.microsoft.com/en-us/library/ms173150.aspx

Anders K.
he meant c++/cli. trypublic ref class CProperty sealed abstract{};
lukas
A static C# class is actually `abstract sealed`, although you can't explicitly use these modifiers together in your code. But the OP is talking about C++/CLI, where there is no concept of static class
Thomas Levesque