why is sealed and static not possible at the same time?

Z

Zytan

I know you cannot have a sealed static class, but why not? Why must
static classes be left open to inheritance?

This article: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/
recommends to place DLL imports into a sealed class, with a private
constructor to prevent instantiation (see Figure 1 at the very top):
http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx?loc=&fig=true#fig1

Why not just make a static class to prevent instantiation? I assume
because then it can be inherited. But, you cannot make it sealed.
Why not?

Zytan
 
J

Jon Skeet [C# MVP]

Zytan said:
I know you cannot have a sealed static class, but why not? Why must
static classes be left open to inheritance?

They're not. Static classes are *implicitly* sealed.
This article: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/
recommends to place DLL imports into a sealed class, with a private
constructor to prevent instantiation (see Figure 1 at the very top):
http://msdn.microsoft.com/msdnmag/issues/03/07/NET/default.aspx?loc=&fig=true#fig1

Why not just make a static class to prevent instantiation?

That article was written in 2003, before static classes existed in C#.
I assume because then it can be inherited. But, you cannot make it
sealed. Why not?

They are sealed, implicitly. Just try inheriting from one:

using System;

static class Base
{
}

class Derived : Base
{
}

Test.cs(7,7): error CS0709: 'Derived': cannot derive from static class
'Base'
 
Z

Zytan

I know you cannot have a sealed static class, but why not? Why must
They're not. Static classes are *implicitly* sealed.

Ok, I was thinking that. I wish the error message merely said that I
was being redundant.
That article was written in 2003, before static classes existed in C#.

Ah! Ok, that explains it. And static classes are a much more elegant
solution than making it sealed and hiding the constructor.

Thanks again, Jon!

Zytan
 
B

Ben Voigt

Ah! Ok, that explains it. And static classes are a much more elegant
solution than making it sealed and hiding the constructor.

static = sealed + abstract (though the C# compiler won't accept sealed
abstract, you must use static, but the resulting MSIL is just the
combination of sealed and abstract)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Ben said:
static = sealed + abstract (though the C# compiler won't accept sealed
abstract, you must use static, but the resulting MSIL is just the
combination of sealed and abstract)

I guess that a sealed abstract class could contain abstract methods, but
they could never be implemented, so that would be pointless.
 
B

Ben Voigt

Göran Andersson said:
I guess that a sealed abstract class could contain abstract methods, but
they could never be implemented, so that would be pointless.

A class can be abstract without having any abstract methods. In standard
C++, you'd do this with protected constructors, in .NET there's an actual
abstract tag on the type.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Ben said:
A class can be abstract without having any abstract methods. In standard
C++, you'd do this with protected constructors, in .NET there's an actual
abstract tag on the type.

Yes, but you are missing the point.

If an sealed abstract class would be allowed, it would be possible to
put abstract methods in it. This would be misleading, as there is no
possible way to use them.
 
Z

Zytan

static = sealed + abstract (though the C# compiler won't accept sealed
abstract, you must use static, but the resulting MSIL is just the
combination of sealed and abstract)

I see. Thanks, Ben

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top