Re: why no struct inheritance?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sun, 6 May 2007 16:28:09 +0100
not_a_commie <notacommie@xxxxxxxxx> wrote:
What's the technical reason for not allowing structs to be
hierarchical?
Well, it would be reasonably feasible to allow inheritance so long as
you didn't want polymorphic methods and didn't add any member
variables. You see, if I declare:
struct Point
{
int x;
int y;
// Other operations
}
and then elsewhere in your code:
Point point;
then the "point" variable has room for 8 bytes - 4 for x, 4 for y. If
we were able to do:
Point point = new ExtendedPoint();
then:
a) the runtime couldn't know the actual type of the value, because
there isn't any room for it
b) there wouldn't be room for any extra member variables of
ExtendedPoint
I'm sure there would be ways round this is value type inheritance had
been desired from the start, but the current design doesn't allow it at
all.
As Bob said, extension methods in C# 3 may well help you.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: why no struct inheritance?
- From: Paul Werkowitz
- Re: why no struct inheritance?
- References:
- why no struct inheritance?
- From: not_a_commie
- why no struct inheritance?
- Prev by Date: Re: XML: empty namespace?
- Next by Date: Cannot convert from byte* to byte[]
- Previous by thread: Re: why no struct inheritance?
- Next by thread: Re: why no struct inheritance?
- Index(es):
Relevant Pages
|