473,757 Members | 7,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
7 1846
Zytan <zy**********@y ahoo.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.co m>
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
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
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
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

"Göran Andersson" <gu***@guffa.co mwrote in message
news:eL******** *****@TK2MSFTNG P04.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
Ben Voigt wrote:
"Göran Andersson" <gu***@guffa.co mwrote in message
news:eL******** *****@TK2MSFTNG P04.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1997
by: Buddy Ackerman | last post by:
I have taken over an application that has a sealed (singleton) class for database access. I want to add a private SQLConnection class variable and open the connection it whenever it is instantiated and close it when the class it disposed. I do this so that I can call mutiple methods within the class without having to open a connection each time. I implemented an IDisposable interface (and a finalize interface) but it doesn't appear to work...
5
7639
by: Bharat Karia | last post by:
Hi, Is it possible to writed Sealed classes in C++ . i.e. there is no sealed/final keyword in C++, but is it possible to achieve the same effect? i.e. deriving from a sealed class is an error and the compiler should flag it as such. Thanks Bharat Karia
14
2942
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
13
8108
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that the code inside the constructor / desconstructor will never run? Mark
9
8409
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
10
3874
by: TJM | last post by:
Hi, Is it possible to have a method sealed and abstract at the same time? MSDN states clearly that this is not allowed for classes but it does not mention it for methods. I tried with a simple example and the compiler would not allow me to compile, however in a recent interview, I was asked this question and the interviewer claimed that this is used in certain situations! I am baffled! Thanks,
3
2110
by: archana | last post by:
Hi all, I have created one sealed class having one constant. Can i access that constant of sealed class without creating object of that class. If yes will internally new object gets created. Or will it behave like static member which remains in memory throughotut applications' lifetime. Please correct me if i am wrong.
18
2958
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent definition is perfectly legal in other CLR languages. For example in C#; static class XXX
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.