473,410 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Sealed classes and constructors / deconstructors

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
Nov 16 '05 #1
13 8074
Mark Rae wrote:
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


Hey Mark,

actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be
extended by other classes by inheritence. For instance if you have a
class like this:

public sealed class MySealed
{
....
}

you cannot do this:

public class SomeClass : MySealed
{
....
}
Nov 16 '05 #2
Mark,

You are mistaken. Sealed classes mean that they can not be derived
from. It doesn't mean that you can't create them. Rather, static classes
can not be derived from or have instances of themselves created.

And if you can't instantiate a class (for whatever reason), then the
constructor and the finalizer is never called.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:uS**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #3
"laimis" <si*****@iit.edu> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be extended
by other classes by inheritence. For instance if you have a class like
this:


Yes, sorry - I got myself confused with sealed and static.

What I meant to ask was that if a sealed class provides only static methods
which are referenced without instiation of the class, does that mean that
even if that class has a constructor, the code inside the constructor will
never fire?
Nov 16 '05 #4
A class that can't be instantiated is called an abstract class (and is
marked with the abstract keyword). An abstract class can have
constructors, and they will get called from the constructors of a
class that inherits from it.

- Magnus

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:uS**************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #5
Mark,

With a static class, you should get a compile error if you try and
define any instance methods, including a constructor, so you shouldn't even
get that far.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ub**************@TK2MSFTNGP14.phx.gbl...
"laimis" <si*****@iit.edu> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be extended
by other classes by inheritence. For instance if you have a class like
this:


Yes, sorry - I got myself confused with sealed and static.

What I meant to ask was that if a sealed class provides only static
methods which are referenced without instiation of the class, does that
mean that even if that class has a constructor, the code inside the
constructor will never fire?

Nov 16 '05 #6
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:em**************@TK2MSFTNGP10.phx.gbl...
You are mistaken. Sealed classes mean that they can not be derived
from. It doesn't mean that you can't create them. Rather, static classes
can not be derived from or have instances of themselves created.
Yes I got myself confused between sealed and static.
And if you can't instantiate a class (for whatever reason), then the
constructor and the finalizer is never called.


That's what I wanted to know - thanks very much.
Nov 16 '05 #7
sealed refers to inheritance, as mentioned already. This
deals with class & library design.

static relates to method access. If a class has a public
ctor, then that ctor will be "called" if the class is
instantiated as an object. If you don't make an instance
of the class, the ctor won't be called.

The above is in re .NET 1.x. In .NET 2, an entire class
can be declared static, in which case there is no ctor, and
the class can't be instantiated.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ub**************@TK2MSFTNGP14.phx.gbl...
"laimis" <si*****@iit.edu> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be extended
by other classes by inheritence. For instance if you have a class like
this:


Yes, sorry - I got myself confused with sealed and static.

What I meant to ask was that if a sealed class provides only static
methods which are referenced without instiation of the class, does that
mean that even if that class has a constructor, the code inside the
constructor will never fire?

Nov 16 '05 #8
What I meant to ask was that if a sealed class provides only static methods
which are referenced without instiation of the class, does that mean that
even if that class has a constructor, the code inside the constructor will
never fire?


Yeah, as long as you don't intantiate the class with new, but call only
static methods, the class constructor will not get called.

Also if you have any static private members that you initialize in the
declaration, there is such thing as static constructor which will get
called when you access the static methods for the first time.

If you have something like:

public class MyClass
{
private static int a = 0;

public static void TestMethod()
{
...
}

.....
}

then static constructor will be generated for you during the compile
time and inside it will initialize a to 0. If you leave the value out of it:

private static int a;

the static constructor will not get generated.
Nov 16 '05 #9
clu
Agree with Iaimis and Nicholas.

Maybe you were confused by a standard usage for sealed classes, which
simulates "static" classes.

static class MyStaticClass {}

is a new feature brought by the v2.0 of the Framework.

In v1.* you can actually mimick such a behaviour writing code like
this:

sealed class MyStaticClass {
private MyStaticClass() {}
}

The private ctor is what truly prevents the object instantiation. The
class is declared ad sealed so that no instance of MyStaticClass can
ever be created, not even inheriting another type from it.

Also notice that, in v1.*, it is perfectly legal to write code like
this:

sealed class MyStaticClass {
private MyStaticClass() {}

public void MyInstanceMethod() {}
}

The compiler is happy about that, although your MyInstanceMethod can
never be called.
With static classes, however, it is not legal to declare instance
methods.

Well, if the programmer is very careful, everything is ok also without
static classes. But that is not always the case.

An example ?

Have you ever heard about the Environment.HasShutdownStarted property ?

The 1.0 version of the .NET Framework had declared that property as an
instance property, whereas the Environemnt class could not be
instantiated at all. So, the property was unusable (unless you use
Reflection, which is however a bit weird).
The 1.1 version fixed the bug.

With static classes, the bug would have been fixed immediately, just
because the compiler would have reported an error !

Hi

Claudio Brotto
MCP, MCAD.NET

Nov 16 '05 #10
Thanks everyone
Nov 16 '05 #11
That would only be with v2 which, I believe, has "static" classes. If you
are doing this in v1, then you can instantiate a class that has only static
members. If you have a constructor that has logic, that code will run.

You can also create a static constructor which runs the first time the class
is accessed.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u7*************@tk2msftngp13.phx.gbl...
Mark,

With a static class, you should get a compile error if you try and
define any instance methods, including a constructor, so you shouldn't even get that far.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ub**************@TK2MSFTNGP14.phx.gbl...
"laimis" <si*****@iit.edu> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
actually sealed classes can be instantiated and have a regular class
behavior in c#. The "sealed" part means that the class cannot be extended by other classes by inheritence. For instance if you have a class like
this:


Yes, sorry - I got myself confused with sealed and static.

What I meant to ask was that if a sealed class provides only static
methods which are referenced without instiation of the class, does that
mean that even if that class has a constructor, the code inside the
constructor will never fire?


Nov 16 '05 #12
Magnus Krisell wrote:
A class that can't be instantiated is called an abstract class (and is
marked with the abstract keyword).


Any class can be prevented from being instantiated if you make a private
constructor.

Eric
Nov 16 '05 #13
Eric <Er**@nospam.com> wrote:
Magnus Krisell wrote:
A class that can't be instantiated is called an abstract class (and is
marked with the abstract keyword).


Any class can be prevented from being instantiated if you make a private
constructor.


.... and make that the *only* constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mei | last post by:
I have some classes at the moment that are initialised with their own function rather than a constructor and destructor. I wish this case to be reversed if possible. For arguments sake (you dont...
6
by: Julie | last post by:
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed keyword in C++, but that there are...
5
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...
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
4
by: Néstor Marcel Sánchez Ahumada | last post by:
In a method declaration the 'sealed' keyword must be used with the 'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why? This would be a good enhancement for...
6
by: Sahil Malik | last post by:
Maybe I am missing something really elementry here - but why can a sealed class not be marked as static? (Even though it can contain all static members?) ... hmmmmm !! PS: This is .NET 2.0 -...
9
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
3
by: Gugale at Lincoln | last post by:
I am using a function to get properies and property values of an object private string ObjPropVals(Object o) { Type t = o.GetType(); PropertyInfo pia = t.GetProperties(); string stra = new...
7
by: Zytan | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.