Constructors

D

DeveloperX

In an attempt to solve a problem further down I suggested the problem
might be caused by a missing constructor. Now, this led me to the
conclusion that I don't fully understand constructors, or have made
lots of assumptions.

Things I (think) know, please correct as necessary:
Constructors are called on instantiation.
Static constructors are called the first time a class is referenced.
There can be many constructors and you can chain them together with :
this()
Using : base() will call the base class's constructor.
If there isn't a constructor the constructor of the class's base will
be called. This will carry on up the chain until it hits object if no
constructors are found.
If the base class only has a parameterized constructor you need to
call it from your derived class as : base(required param)
The constructor of the base class is always called before the derived
class's constructor.

So why do I see every where:

public class Test
{
public Test()
{
//do nothing
}
//class stuff
}

what is the point of a public constructor that does nothing? I do it
all the time without ever thinking about it, and it seems I'm not
alone. I could understand if it was protected or private, but public?
 
V

VJ

are you asking why the template that Visual Studio puts has a public
construtor?...If yes, thats just a template, you can change to what you
need. Some people who don't use it leave it as is..

Your understand of constructors look ok to me!

VJ
 
C

Christof Nordiek

Hi DeveloperX,

DeveloperX said:
In an attempt to solve a problem further down I suggested the problem
might be caused by a missing constructor. Now, this led me to the
conclusion that I don't fully understand constructors, or have made
lots of assumptions.

Things I (think) know, please correct as necessary:
Constructors are called on instantiation. Yes.
Static constructors are called the first time a class is referenced.

In priciple, yes.
There can be many constructors

constructors can be overloaded, like methods.
and you can chain them together with :
this()
Using : base() will call the base class's constructor.

Each constructor has to call anothter constructor before it's own code.
Either of the same class :)this(...)) or of the direct base class
:)base(...))
If this constructor is not explicitly set in code, the the parameterless
constructor of the baseclass is called.
So inside of class A following declarations are equivalent:

A ()
{}

and

A ():base()
{}
If there isn't a constructor the constructor of the class's base will
be called.

If no constructor is explicitly declared a parameterless constructor is
implicitly defined, wich calls the parameterless constructor of the direct
base class.
This will carry on up the chain until it hits object if no
constructors are found.

The principal, you have in mind, follows from what I said above.
If the base class only has a parameterized constructor you need to
call it from your derived class as : base(required param)

If the base class has no parameterized constructor, your bound to call the
baseclass constructor explicitly.
The constructor of the base class is always called before the derived
class's constructor.

Yes, though method calls, in the argumentlist will be executed before the
baseclass constructor.
So why do I see every where:

public class Test
{
public Test()
{
//do nothing
}
//class stuff
}

As you supposed, this conatructor declaration does nothing, since an
equivalent constructor would be generated explicitly.
But if you declared an additional constructor (with parameter), this
constructor would disappear.
Also such constructor could be provided by a template or wizzard, so that
code could be entered easily

Regards
Christof
 
J

Jon Skeet [C# MVP]

In an attempt to solve a problem further down I suggested the problem
might be caused by a missing constructor. Now, this led me to the
conclusion that I don't fully understand constructors, or have made
lots of assumptions.

Static constructors are called the first time a class is referenced.

Ish. The exact rules are slightly complicated. Note that if you have
static variable initializers but no static constructor, the rules are
slightly different again.
what is the point of a public constructor that does nothing?

On its own - none. If you have no other constructors, you can just
leave it out. However, if you have any constructors with parameters,
the default constructor won't be provided for you, so if you *also*
want a parameterless constructor, you need to specify it.

Jon
 
D

DeveloperX

Ish. The exact rules are slightly complicated. Note that if you have
static variable initializers but no static constructor, the rules are
slightly different again.


On its own - none. If you have no other constructors, you can just
leave it out. However, if you have any constructors with parameters,
the default constructor won't be provided for you, so if you *also*
want a parameterless constructor, you need to specify it.

Jon

Thanks everyone, I'll do some reading up on static constructors/
variable initializers and should feel a bit better about it. It's
remarkable what you can take for granted.
 
J

Jon Skeet [C# MVP]

Thanks everyone, I'll do some reading up on static constructors/
variable initializers and should feel a bit better about it. It's
remarkable what you can take for granted.

See http://pobox.com/~skeet/csharp/beforefieldinit.html for my take on
it.

An example of "referencing" a type without it calling a static
constructor is:

using System;

class Foo
{
static Foo()
{
Console.WriteLine ("Foo initialized");
}
}

class Test
{
static void Main()
{
Console.WriteLine (typeof(Foo));
}
}

Jon
 
D

DeveloperX

Seehttp://pobox.com/~skeet/csharp/beforefieldinit.htmlfor my take on
it.

An example of "referencing" a type without it calling a static
constructor is:

using System;

class Foo
{
static Foo()
{
Console.WriteLine ("Foo initialized");
}

}

class Test
{
static void Main()
{
Console.WriteLine (typeof(Foo));
}

}

Jon

Comically I tried to use this to resolve the config issue, now I
understand why it didn't work. I wanted the debugger to break on first
reference, but I assume the way it's trying to instantiate the object,
it's breaking at the acquisition of the type. We've had a lot of this
using the Ent library, but it happens that infrequently that I'm
damned if I can formalise it in my own mind.

Still, live and learn :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top