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
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
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
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
"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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
by: Zeng |
last post by:
Would somebody know when we should seal a class? Shouldn't all classes be
open up for inheritance?
Thanks!
|
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
|
by: Kylin |
last post by:
any better reason ?
--
FireCrow Studio
Kylin Garden
EMail:gaotianpu@gmail.com
ICQ:156134382
| |
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,
|
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.
|
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
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |