> Please anybody explain me (C#):
> 1. Why default constructors of structure types cannot be defined?
Because they wouldn't always be called. For instance:
MyStruct[] x = new MyStruct[100];
Would you want that to call the parameterless constructor 100 times? It
would be a bit of a waste if you were about to overwrite all the
values.
What about just:
MyStruct y;
?
I seem to remember that there are some much odder cases, too. You could
say that the constructor would only *sometimes* be called, of course -
but that would limit their usefulness significantly.
> 2. Why finalize method cannot be overrided in structure types?
Structures aren't garbage collected unless they're boxed. The space is
just freed.
> 3. Why structure type cannot be used as a base type?
You'd get some odd (well, for some people) behaviour if you allowed
that. Consider the following code:
struct Base
{
int x;
}
struct Derived : Base
{
int y;
}
void SomeMethod()
{
Base b = new Derived();
}
How much space should be allocated on the stack for the variable b?
Base only takes up 4 bytes, but Derived takes up 8. You'd have to
either disallow this, or only *actually* use a Base even though you've
asked for a Derived.
There *are* lots of options around here (as native C++ handles this
case, of course), but .NET makes things arguably simpler by disallowing
it in the first place.
I get the feeling Ben Voigt is going to reply to this, if he reads it
:)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
count1986@gmail.com - 21 Sep 2007 20:54 GMT
Laura T. - 21 Sep 2007 21:00 GMT
>> 3. Why structure type cannot be used as a base type?
>
[quoted text clipped - 20 lines]
> either disallow this, or only *actually* use a Base even though you've
> asked for a Derived.
Actually, your example could be read as C++ union datatype. Derived is a
union of base and derived.
Syntax is nice clean altough a bit strange at first sight. It could work.
:-)
>> Please anybody explain me (C#):
>> 1. Why default constructors of structure types cannot be defined?
[quoted text clipped - 53 lines]
> I get the feeling Ben Voigt is going to reply to this, if he reads it
> :)
Jon Skeet [C# MVP] - 21 Sep 2007 21:32 GMT
> Actually, your example could be read as C++ union datatype. Derived is a
> union of base and derived.
> Syntax is nice clean altough a bit strange at first sight. It could work.
> :-)
From what I remember, a union uses two variables for a single area of
storage. In my example, I'd expect x and y to be *independent*
variables.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too