Questions

  • Thread starter Thread starter count1986
  • Start date Start date
C

count1986

Please anybody explain me (C#):
1. Why default constructors of structure types cannot be defined?
2. Why finalize method cannot be overrided in structure types?
3. Why structure type cannot be used as a base type?
 
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
:)
 
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.

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.
:-)


Jon Skeet said:
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
:)
 
Laura T. said:
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.
 
Back
Top