> I'd like to send in an instance of the struct and pass in a
> value to a constructor. However, constructors are not
> allowed for structs.
What makes you say that? It's not true.
> As far i see i can't do the following.
>
> doSomething (new MyStruct (1));
>
> My compiler complains.
Well, as far as the code you posted goes, of course it would. There's no
declaration for MyStruct, nor for doSomething(). My compiler would
complain too.
Now, if you have some other code, the context in which the above line
_would_ otherwise compile (that is, it exists in a larger setting in which
MyStruct and doSomething() are actually legitimate, declared identifiers),
you should show that code. Likewise, you should be more specific about
what's not working than "my compiler complains". There's no way for
anyone to just guess at what you're doing wrong. You have to _show_ us.
But there's nothing fundamentally wrong with a line of code that looks
like what you posted, assuming you've correctly declared the struct and
the method being called.
Pete
K Viltersten - 20 Mar 2008 22:36 GMT
>> I'd like to send in an instance of the struct and pass in a
>> value to a constructor. However, constructors are not
>> allowed for structs.
>
> What makes you say that? It's not true.
To answer your question - the error message i get from
the compiler. It read exactly:
"Structs cannot contain explicit parameterless constructors"
See, now read it omitting the word "parameterless". Note,
you need to OMIT it. Otherwise, it might sound like if one
actually could declare constructors for structs. :)
In plain text - i pulled a donkey. Sorry for confusing both of us.
Thanks for the help.
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Rene - 21 Mar 2008 03:11 GMT
> "Structs cannot contain explicit parameterless constructors"
A struct inherently contains a parameterless constructor, it must do so
because if it didn’t then how would you be able to do something like the
following:
MyStruct[] myArray = new MyStruct[100];
If the struct didn’t have the parametrless constructor the array above could
not possible be created.
The question ofcourse would be, why won't the compiler allow you to define
your own parameterless constructor and override the default one...... the
answer to that is..... I duno!
Peter Duniho - 21 Mar 2008 03:40 GMT
> [...]
> The question ofcourse would be, why won't the compiler allow you to
> define your own parameterless constructor and override the default
> one...... the answer to that is..... I duno!
I don't know the answer myself, but if I had to guess, it'd be that it's
because the default constructor for a struct basically always fills the
struct with zeros. This means that the compiler knows that it doesn't
have to do anything special for an array of structs; it's just filled with
zeros the way any memory allocated from the heap would be. Likewise for
structs found as fields in classes (which also get filled with zeros, not
counting instance-initialization -- i.e. parameterless constructor or
field initializers -- that _is_ supported for classes and so which might
occur).
If the language supported defining your own parameterless constructor (or
field initializers, which are also not allowed in structs), it would
introduce a special case in which the compiler sometimes would have to
generate a bunch of code to iterate over an array, or to add implicit
calls to the instance-initialization code for class members that are
structs. It makes sense to me that rather than introduce that
complication, the language designers just decided to forego the utility of
user-defined default constructors. It was a trade-off in which simplicity
of the language and compiler won (which IMHO is generally the right way to
go, though I'm sure not everyone agrees).
Basically, value types remain nice and simple with minimal overhead and
consistent, predictable behavior. If you want more complex behavior, you
need a class. It seems like a reasonable rule to me.
Pete
Jon Skeet [C# MVP] - 21 Mar 2008 09:43 GMT
> > [...]
> > The question ofcourse would be, why won't the compiler allow you to
[quoted text clipped - 6 lines]
> have to do anything special for an array of structs; it's just filled with
> zeros the way any memory allocated from the heap would be.
Bingo. That's also the case when the struct is used as a member
variable - types are initialized by just wiping out memory.
One interesting (to me anyway) point is that from a *language* point of
view all value types have a parameterless constructor - but from a
*runtime* point of view, value types *never* have a parameterless
constructor. (They're initialized with a different op-code.) You can
see this by trying to fetch the constructors for a value type using
reflection - you won't see the parameterless one.)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk