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