By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/is...&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

Mar 9 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Zytan <zy**********@yahoo.comwrote:
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/is...&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'

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 9 '07 #2

P: n/a
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.
Ok, I was thinking that. I wish the error message merely said that I
was being redundant.
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/is...ult.aspx?loc=&...
Why not just make a static class to prevent instantiation?

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

Mar 9 '07 #3

P: n/a
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)
>
Thanks again, Jon!

Zytan

Mar 9 '07 #4

P: n/a
Ben Voigt wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 9 '07 #5

P: n/a

"Göran Andersson" <gu***@guffa.comwrote in message
news:eL*************@TK2MSFTNGP04.phx.gbl...
Ben Voigt wrote:
>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.
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.
>
--
Göran Andersson
_____
http://www.guffa.com

Mar 9 '07 #6

P: n/a
Ben Voigt wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:eL*************@TK2MSFTNGP04.phx.gbl...
>Ben Voigt wrote:
>>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.

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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 9 '07 #7

P: n/a
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

Mar 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.