Sealed classes and constructors / deconstructors

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

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
 
Mark said:
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
{
....
}
 
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.
 
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?
 
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,

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.
 
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.
 
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
 
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.
 
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
 
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 said:
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]
- (e-mail address removed)

Mark Rae said:
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 said:
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
 
Back
Top