Value type and a parameterless constructor

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."

Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?
 
For value types you can't explicitly create a parameterless constructor,
because the compiler doest that.

Yes, you will still have a parameterless constructor provided by the
compiler.
 
Well, when you created a value type with some other constructor, did it  
also wind up with a parameterless constructor?

This is trivial to test yourself, so you should just do that.

Ah, I did write a rudimentary test after i posted the question... I
was more interested as to why parameterless ctor is included,
reasoning behind that is -- that my compiler couldn't explain :)

Thanks
 
Each constructor for a value type *MUST *initialize all the fields or it
must call some other constructor which does initialize all other fields.

eg: This code doesn't compile
struct Test
{
int a;
int b;

Test(int x)
{
a = x;
}

}

However this code compiles fine

struct Test
{
int a;
int b;
Test(char c)
{
a = b = -1;
}
Test(int x) : this('c')
{
a = x;
}
}
 
Peter said:
[HTML post snipped]

Please stop posting in HTML. It's considered an abuse of the Usenet
messaging system.

I can't say I really mind it, because his client is doing the right thing
and posting both HTML and plain text attachments as multipart/alternative.
If your client is displaying the HTML attachment and you don't like that, it
should have an option for picking your desired format. Clients that don't
understand MIME at all might have a problem, but those should be rare to
nonexistent by now.

I'm actually more annoyed by the top-posting...
 
Ashutosh Bhawasinka said:
For value types you can't explicitly create a parameterless constructor,
because the compiler doest that.

Yes, you will still have a parameterless constructor provided by the
compiler.

In fact, for value types the compiler doesn't create a constructor in
the IL. This is an interesting impedance mismatch between the CLI spec
and the C# spec - as far as C# is concerned, *all* value types have a
parameterless constructor. As far as the CLI spec is concerned, *no*
value types have a parameterless constructor (you can't define one).
That's why if you fetch the constructors via reflection, you won't see
the parameterless one. However, all value types allow you to create an
instance without specifying parameters - it just uses a different op
code, meaning "wipe the data to 0" basically.
 
Peter Duniho said:
It's actually a bit of cheat, because the default constructor isn't really
called in those scenarios. If you look at the definition of the default
values for value types, you'll see that they are all in some way "zero".
So what really happens is that the run-time allocates memory that's been
zero-initialized. But by defining value type operations in a way that
ensures that they will always be initialized to zero, this allows that
easy implementation to meet the specification.

It's even more interesting than that, in terms of the allocation. I
looked into this for a recent StackOverflow question. (Apologies to all
for my woeful neglect of this newsgroup. StackOverflow is proving
somewhat addictive.) Anyway:

http://stackoverflow.com/questions/203695/structure-vs-class-in-c
 
Back
Top