Connecting Tech Pros Worldwide Forums | Help | Site Map

Sealed classes and constructors / deconstructors

Mark Rae
Guest
 
Posts: n/a
#1: Nov 16 '05
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



laimis
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Sealed classes and constructors / deconstructors


Mark Rae wrote:[color=blue]
> 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
>
>[/color]

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
{
....
}


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Sealed classes and constructors / deconstructors


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]
- mvp@spam.guard.caspershouse.com

"Mark Rae" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:uSkkDyEEFHA.3504@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>[/color]


Mark Rae
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Sealed classes and constructors / deconstructors


"laimis" <simulai@iit.edu> wrote in message
news:erMhs2EEFHA.3728@TK2MSFTNGP14.phx.gbl...
[color=blue]
> 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:[/color]

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?


Magnus Krisell
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Sealed classes and constructors / deconstructors


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" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:uSkkDyEEFHA.3504@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Sealed classes and constructors / deconstructors


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]
- mvp@spam.guard.caspershouse.com

"Mark Rae" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ubRWU6EEFHA.3728@TK2MSFTNGP14.phx.gbl...[color=blue]
> "laimis" <simulai@iit.edu> wrote in message
> news:erMhs2EEFHA.3728@TK2MSFTNGP14.phx.gbl...
>[color=green]
>> 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:[/color]
>
> 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?
>[/color]


Mark Rae
Guest
 
Posts: n/a
#7: Nov 16 '05

re: Sealed classes and constructors / deconstructors


"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:emDgY4EEFHA.3512@TK2MSFTNGP10.phx.gbl...
[color=blue]
> 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.[/color]

Yes I got myself confused between sealed and static.
[color=blue]
> And if you can't instantiate a class (for whatever reason), then the
> constructor and the finalizer is never called.[/color]

That's what I wanted to know - thanks very much.


Peter N Roth
Guest
 
Posts: n/a
#8: Nov 16 '05

re: Sealed classes and constructors / deconstructors


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" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ubRWU6EEFHA.3728@TK2MSFTNGP14.phx.gbl...[color=blue]
> "laimis" <simulai@iit.edu> wrote in message
> news:erMhs2EEFHA.3728@TK2MSFTNGP14.phx.gbl...
>[color=green]
>> 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:[/color]
>
> 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?
>[/color]


laimis
Guest
 
Posts: n/a
#9: Nov 16 '05

re: Sealed classes and constructors / deconstructors


[color=blue]
> 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?
>[/color]

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.


clu
Guest
 
Posts: n/a
#10: Nov 16 '05

re: Sealed classes and constructors / deconstructors


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

Mark Rae
Guest
 
Posts: n/a
#11: Nov 16 '05

re: Sealed classes and constructors / deconstructors


Thanks everyone


Peter Rilling
Guest
 
Posts: n/a
#12: Nov 16 '05

re: Sealed classes and constructors / deconstructors


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]" <mvp@spam.guard.caspershouse.com> wrote in
message news:u7eo5$EEFHA.228@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
even[color=blue]
> get that far.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "Mark Rae" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
> news:ubRWU6EEFHA.3728@TK2MSFTNGP14.phx.gbl...[color=green]
> > "laimis" <simulai@iit.edu> wrote in message
> > news:erMhs2EEFHA.3728@TK2MSFTNGP14.phx.gbl...
> >[color=darkred]
> >> actually sealed classes can be instantiated and have a regular class
> >> behavior in c#. The "sealed" part means that the class cannot be[/color][/color][/color]
extended[color=blue][color=green][color=darkred]
> >> by other classes by inheritence. For instance if you have a class like
> >> this:[/color]
> >
> > 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?
> >[/color]
>
>[/color]


Eric
Guest
 
Posts: n/a
#13: Nov 16 '05

re: Sealed classes and constructors / deconstructors


Magnus Krisell wrote:[color=blue]
> A class that can't be instantiated is called an abstract class (and is
> marked with the abstract keyword).[/color]

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

Eric
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#14: Nov 16 '05

re: Sealed classes and constructors / deconstructors


Eric <Eric@nospam.com> wrote:[color=blue]
> Magnus Krisell wrote:[color=green]
> > A class that can't be instantiated is called an abstract class (and is
> > marked with the abstract keyword).[/color]
>
> Any class can be prevented from being instantiated if you make a private
> constructor.[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread