Terminology

  • Thread starter Thread starter Peter Kirk
  • Start date Start date
P

Peter Kirk

Hi there

I just want to make sure I have some basic terminology 100% correct. It
concerns the term "default constructor" vs "no argument constructor".

In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?

And while the "default constructor" is a constructor with no arguments, if
the programmer supplies a no-argument constructor then this constructor is
NOT a "default constructor" - it is a "no-argument" constructor?

(So is there a difference between a "default constructor" and a "no
argument" constructor? And is there an accepted name for a "no argument"
constructor?)


Thanks,
Peter
 
Peter, this is the MSIL for a specified no-argument constructor...

..method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


If I don't specifiy one, the MSIL looks like this...

..method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


So there's no difference at the .Net level. Whether or not there is a
difference at the C# specification level, I'm not sure. IMHO,
differentiating between default and no-argument constructors is pointless. I
come from a C++ background, so perhaps I'm biased. VB developers (read
script kiddies ;) ) may have a different take on it.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
Hi,

Peter said:
Hi there

I just want to make sure I have some basic terminology 100% correct. It
concerns the term "default constructor" vs "no argument constructor".

In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?
Yes.


And while the "default constructor" is a constructor with no arguments, if
the programmer supplies a no-argument constructor then this constructor is
NOT a "default constructor" - it is a "no-argument" constructor?
Yes.


(So is there a difference between a "default constructor" and a "no
argument" constructor? And is there an accepted name for a "no argument"
constructor?)

There is a difference. The default constructor is automatically generated and
just initializes the fields to default values. A class constructor with no
parameters can contain code to initialize the object in whatever way makes sense.

The MSDN documentation seems to refer to constructors with no parameters
(whether or not they are default) as "parameterless" constructors.
Thanks,
Peter

Best regards,

Rodger

Achieve Planner time managment software - Project/task outliner with calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
Of course, like Rodger says, you can put code in a non default parameterless
ctor. I was speaking from the perspective of not putting any code in it.

Tim Haughton

Tim Haughton said:
Peter, this is the MSIL for a specified no-argument constructor...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


If I don't specifiy one, the MSIL looks like this...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


So there's no difference at the .Net level. Whether or not there is a
difference at the C# specification level, I'm not sure. IMHO,
differentiating between default and no-argument constructors is pointless. I
come from a C++ background, so perhaps I'm biased. VB developers (read
script kiddies ;) ) may have a different take on it.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton


Peter Kirk said:
Hi there

I just want to make sure I have some basic terminology 100% correct. It
concerns the term "default constructor" vs "no argument constructor".

In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?

And while the "default constructor" is a constructor with no arguments, if
the programmer supplies a no-argument constructor then this constructor is
NOT a "default constructor" - it is a "no-argument" constructor?

(So is there a difference between a "default constructor" and a "no
argument" constructor? And is there an accepted name for a "no argument"
constructor?)


Thanks,
Peter
 
Hi Tim,

Yes, I realized after seeing your post that is what Peter was asking :) The only
other difference I can think of without any code in it is that you can make it
private to prevent it from being used outside the class.

You could also call base class constructors with different values, but that is
sort of like putting code in it.

Best regards,

Rodger

Tim said:
Of course, like Rodger says, you can put code in a non default parameterless
ctor. I was speaking from the perspective of not putting any code in it.

Tim Haughton

Peter, this is the MSIL for a specified no-argument constructor...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


If I don't specifiy one, the MSIL looks like this...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


So there's no difference at the .Net level. Whether or not there is a
difference at the C# specification level, I'm not sure. IMHO,
differentiating between default and no-argument constructors is pointless.
I

come from a C++ background, so perhaps I'm biased. VB developers (read
script kiddies ;) ) may have a different take on it.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton


Hi there

I just want to make sure I have some basic terminology 100% correct. It
concerns the term "default constructor" vs "no argument constructor".

In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?

And while the "default constructor" is a constructor with no arguments,
if
the programmer supplies a no-argument constructor then this constructor
is
NOT a "default constructor" - it is a "no-argument" constructor?

(So is there a difference between a "default constructor" and a "no
argument" constructor? And is there an accepted name for a "no argument"
constructor?)


Thanks,
Peter
 
Hi, and thanks for the answers. Actually I wasn't really asking about the
possibility of putting code in a parameterless constructor you write
yourself - I was interested in what one calls these beasts.

If, for example, I was to receive a task described such: "You must supply a
default constructor and a constructor which takes an IReader object". Then
technically speaking I can say that this is not possible - because I cannot
supply a "default constructor" (the compiler does that) and if I write a
constructor which takes an IReader object then there will in fact not be a
default constructor. The best I could do would be to supply a "parameterless
constructor".

Only if I wanted to be a nit-picking a*** of course.

Peter


Rodger Constandse said:
Hi Tim,

Yes, I realized after seeing your post that is what Peter was asking :)
The only other difference I can think of without any code in it is that
you can make it private to prevent it from being used outside the class.

You could also call base class constructors with different values, but
that is sort of like putting code in it.

Best regards,

Rodger

Tim said:
Of course, like Rodger says, you can put code in a non default
parameterless
ctor. I was speaking from the perspective of not putting any code in it.

Tim Haughton

Peter, this is the MSIL for a specified no-argument constructor...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


If I don't specifiy one, the MSIL looks like this...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Class1::.ctor


So there's no difference at the .Net level. Whether or not there is a
difference at the C# specification level, I'm not sure. IMHO,
differentiating between default and no-argument constructors is
pointless.
I

come from a C++ background, so perhaps I'm biased. VB developers (read
script kiddies ;) ) may have a different take on it.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton



Hi there

I just want to make sure I have some basic terminology 100% correct. It
concerns the term "default constructor" vs "no argument constructor".

In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?

And while the "default constructor" is a constructor with no arguments,
if

the programmer supplies a no-argument constructor then this constructor
is

NOT a "default constructor" - it is a "no-argument" constructor?

(So is there a difference between a "default constructor" and a "no
argument" constructor? And is there an accepted name for a "no argument"
constructor?)


Thanks,
Peter
 
I would say there is no difference. They are also known as 'implicit
constructors'
But use 'default constructor' as most people are familiar with that.

- Michael S
 
In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?

Yes - "default constructor" is the term the C# Language Specification uses.
And while the "default constructor" is a constructor with no arguments, if
the programmer supplies a no-argument constructor then this constructor is
NOT a "default constructor" - it is a "no-argument" constructor?

I didn't find this distinction anywhere in the spec - I think most people
still say this is the default ctor.
 
Default constructor refers to the fact that it the constructor called or
defaulted to when no arguments are supplied. (See
http://www.c-sharpcorner.com/Language/ConsNDestructorsInCSRVS.asp)

Yes, a no-argument constructor is a default constructor. Yes, the compiler
generated constructor (assuming you don't supply any constructor) is a
default constructor. But, is you supply your own no-argumnet constructor,
that is a default constructor.

I believe the term came from C++. In C++ this is used in many cases where
you need to be able to create an item without knowing anything about how to
create it. Many lists require a default constructor so it can manage the
memory for you. I can create an item using its default constructor, then
using assignment make a shallow copy of one object to another. And yet, I
really know nothing about the object I just created and copied.

Hope this helps...

Frisky
 
I'm with the majority view so far on this.
Just because you implement OR just simply specify the default constructor
does not change the fact that it is still an objects default constructor
(i.e. the method that is called by default). I *believe* (but would have to
look it up) that this stands true in OOP theory.

Br,

Mark.
 
Peter Kirk said:
I just want to make sure I have some basic terminology 100% correct. It
concerns the term "default constructor" vs "no argument constructor".

Ah, that old chestnut.
In c# is it correct that the "default constructor" is a no-argument
constructor which is provided by the .NET platform for a class if the
programmer does not explicitly provide a constructor?

And while the "default constructor" is a constructor with no arguments, if
the programmer supplies a no-argument constructor then this constructor is
NOT a "default constructor" - it is a "no-argument" constructor?

(So is there a difference between a "default constructor" and a "no
argument" constructor? And is there an accepted name for a "no argument"
constructor?)

It's not entirely clear whether the C# language specification would
count an explicitly provided parameterless, empty-bodied constructor as
a default constructor. Personally, I believe it makes things clearer if
the two concepts are separated (so that a "default constructor" is
*only* a constructor provided automatically by the compiler) but that
isn't globally accepted terminology :(

"No argument" is often referred to as "parameterless" though.
 
My last call. This thread have bugged me for hours.

When it comes to CIL and the JITter, there must be a constructor. Or not?

You may call it an 'default-' or an 'implicit-' or why not a
'default-non-parameterized-no-argument-hidden-implicit-' constructor .

CIL and the JITer doesn't care what you call it.

I think that instead of giving something obvious cool names, it is better to
know that any instance in .NET will be constructed. And it is constructed by
default values.

Now lets take a look at these defaults, shall we?

All bools becomes false
All integers becomes zero.
All references becomes null.

Why is this is so? Well, the CLR allocate space for an object and simply
fills that space with zeroes. zero is false and zero is null in the runtime.

QED - Quite Easily Done, and something OO-compilers (not to mention every
graduate-student that wanna score on an exam in compiler-developement) have
done for years as the easy way to initialize objects. This is done
regardless of constructors.

As far as I know, the C# specification does not apply and is of no use here.
CIL and the JITter will fill any instance (class or struct) with zeroes and
then (maybe) call a constructor. If the pointer to the constructor is null
(zero) the JITter would not eject code that would make the CLR call the
constructor (which would make the hardware in your computer signal a IRQ of
calling the adress of zero, which is very much not allowed. *s*).

Writing an empty constructor would not bloat the actual native code.

You can learn a lot about object-creation by reading the code in the Delphi.
Or better, the code in the Mono-project.

I think that the constructor should always be considered as being there,
however implicit. Like you can define a class and don't need to tell that it
should derive from Object. It Must!

It's really that simple.

Happy Coding
- Michael S
 
Back
Top