why do structs not allow default constructors?

  • Thread starter Thread starter Peter Rilling
  • Start date Start date
P

Peter Rilling

Hi.

Two of my biggest complaints about structure in c# is that 1) You cannot
implement a default constructor and 2) You cannot initialize fields on the
declaration line.

I would like to know why this is. Is there some technical reason why C#
choose not to allow you to do the above, or was that decision arbitrary?
 
I once asked this same question of Anders Hejlsberg, face to face during a
..NET pre-launch session in Seattle sometime in 1999.

He told me that there had to be a distinct difference between classes and
structs. The line between them is fine anyway from the point of view of the
compiler and they just turned out that way.

A man of vision...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Peter Rilling said:
Hi.

Two of my biggest complaints about structure in c# is that 1) You cannot
implement a default constructor and 2) You cannot initialize fields on
the declaration line.

I would like to know why this is. Is there some technical reason why C#
choose not to allow you to do the above, or was that decision arbitrary?

Well, default constructor syntax is used to create the "empty" structure, so
allowing the definition of a default constructor woudl take away that
ability.

I'm pretty sure there was also concern that allowing default constructors
would give people the impression that the default constructor would always
be called, something the langauge wasn't able to gaurentee,b ut I can't find
a link to that article.
 
There's also the difficulty of what this means, if a structure has a
default constructor:

System.Drawing.Point p;

Does it mean that "p" will be initialized by the default constructor,
even though you didn't say "= new Point()"? Or does it mean that the
fields in "p" will be set to all zeros and nulls, which would make the
above line different from:

System.Drawing.Point p = new Point();

? What if you don't supply a default constructor... are you still
allowed to say, simply

System.Drawing.Point p;

and what does that mean in the absence of a default constructor?

Not allowing a default constructor (or, if you wish, forcing a
system-defined default constructor on you) clears all of this up. Of
course, I'm sure that there are other solutions, too, but the current
solution isn't such a bad one when you consider the confusion that
could result otherwise.
 
Daniel O'Connell said:
Well, default constructor syntax is used to create the "empty" structure,
so allowing the definition of a default constructor woudl take away that
ability.

I'm pretty sure there was also concern that allowing default constructors
would give people the impression that the default constructor would always
be called, something the langauge wasn't able to gaurentee,b ut I can't
find a link to that article.

I wrote about this a while back

http://www.dotnetconsult.co.uk/weblog/commentview.aspx/03805a0c-525f-4b7d-b0a0-f5f2773b4a7c

The other thing is that the value type may have been received from unmanaged
code - in that case you cannot assume your default constructor will have run

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 

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

Back
Top