views:

1566

answers:

12

I know that structs in .NET do not support inheritance, but its not exactly clear why they are limited in this way.

What technical reason prevents structs from inheriting from other structs?

+4  A: 

Here's what the docs say:

Structs are particularly useful for small data structures that have value semantics. Complex numbers, points in a coordinate system, or key-value pairs in a dictionary are all good examples of structs. Key to these data structures is that they have few data members, that they do not require use of inheritance or referential identity, and that they can be conveniently implemented using value semantics where assignment copies the value instead of the reference.

Basically, they're supposed to hold simple data and therefore do not have "extra features" such as inheritance. It would probably be technically possible for them to support some limited kind of inheritance (not polymorphism, due to them being on the stack), but I believe it is also a design choice to not support inheritance (as many other things in the .NET languages are.)

On the other hand, I agree with the benefits of inheritance, and I think we all have hit the point where we want our struct to inherit from another, and realize that it's not possible. But at that point, the data structure is probably so advanced that it should be a class anyway.

Blixt
That is not the reason why there is no inheritance.
Dykam
Doesn't answer the question.
Matt Howells
I believe the inheritance being talked about here is not being able to use two structs where one inherits from the other interchangeably, but re-using and adding to the implementation of one struct to another (i.e. creating a `Point3D` from a `Point2D`; you would not be able to use a `Point3D` instead of a `Point2D`, but you wouldn't have to reimplement the `Point3D` entirely from scratch.) That's how I interpreted it anyways...
Blixt
In short: it *could* support inheritance without polymorphism. It doesn't. I believe it's a design choice to help a person choose `class` over `struct` when appropriate.
Blixt
+8  A: 

Structs do not use references (unless they are boxed, but you should try to avoid that) thus polymorphism isn't meaningful since there is no indirection via a reference pointer. Objects normally live on the heap and are referenced via reference pointers, but structs are allocated directly on the stack (unless they are boxed).

Martin Liversage
one does not need to use polymorphism to take advantage of inheritance
rmeador
So, you'd have how many different types of inheritance in .NET?
John Saunders
Polymorphism does exist in structs, just consider the difference between calling ToString() when you implement it on a custom struct or when a custom implementation of ToString() does not exist.
kek444
That's because they all derive from System.Object. It's more the polymorphism of the System.Object type than of structs.
John Saunders
A: 

struct type in .net framework is falls under value types. Value types uses stack memory as well as size of value types is fixed.

adatapost
Value typoes can also exist on the heap. And it does not answer the question.
Henk Holterman
Read all posts and then think.
adatapost
If the answer is found in the other posts, why does was this one added (not removed)?
Manu
+2  A: 

Class like inheritance is not possible, as a struct is laid directly on the stack. An inheriting struct would be bigger then it parent, but the JIT doesn't know so, and tries to put too much on too less space. Sounds a little unclear, let's write a example:

struct A {
    int property;
} // sizeof A == sizeof int

struct B : A {
    int childproperty;
} // sizeof B == sizeof int * 2

If this would be possible, it would crash on the following snippet:

void DoSomething(A arg){};

...

B b;
DoSomething(b);

Space is allocated for the sizeof A, not for the sizeof B.

Dykam
C++ handles this case just fine, IIRC. The instance of B is sliced to fit in the size of an A. If it's a pure data type, as .NET structs are, then nothing bad will happen. You do run into a bit of a problem with a method that returns an A and you're storing that return value in a B, but that shouldn't be allowed. In short, the .NET designers _could_ have dealt with this if they wanted to, but they didn't for some reason.
rmeador
For your DoSomething(), there isn't likely to be a problem as (assuming C++ semantics) 'b' would be "sliced" to create an A instance.The problem is with <i>arrays</i>. Consider your existing A and B structs, and a <c>DoSomething(A[] arg){arg[1].property = 1;}</c> method. Since arrays of value types store the values "inline", DoSomething(actual = new B[2]{}) will cause actual[0].childproperty to be set, not actual[1].property. This is bad.
jonp
C++ is not C#
John Saunders
@John: I wasn't asserting it was, and I don't think @jonp was either. We were merely mentioning that this problem is old and has been solved, so the .NET designers chose not to support it for some reason other than technical infeasibility.
rmeador
It should be noted that the "arrays of derived types" issue isn't new to C++; see http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.4(Arrays in C++ are evil! ;-)
jonp
@John: the solution to "arrays of derived types and base types don't mix" problem is, as usual, Don't Do That. Which is why arrays in C++ are evil (more easily permits memory corruption), and why .NET doesn't support inheritance with value types (compiler and JIT ensure that it can't happen).
jonp
Thanks for making my point: C++ is not C#.
John Saunders
I can think of at least two sensible ways for the compiler to handle that that would never lead to a crash. Of course I can think of lots of ways to implement it that will crash, but why would you want to imlpement it that way?
nikie
+21  A: 

Imagine structs supported inheritance. Then declaring:

BaseStruct a;
InheritedStruct b; //inherits from BaseStruct, added fields, etc.

a = b; //?? expand size during assignment?

would mean struct variables don't have fixed size, and that is why we have reference types.

Even better, consider this:

BaseStruct[] baseArray = new BaseStruct[1000];

baseArray[500] = new InheritedStruct(); //?? morph/resize the array?
kek444
C++ answered this by introducing the concept of 'slicing', so that's a solvable problem. So, why shouldn't struct inheritance be supported?
jonp
Consider arrays of inheritable structs, and remember that C# is a (memory)managed language. Slicing or any similar option would wreak havoc on the fundamentals of the CLR.
kek444
@jonp: Solvable, yes. Desirable? Here's a thought experiment: imagine if you have a base class Vector2D(x, y) and derived class Vector3D(x, y, z). Both classes have a Magnitude property which calculates sqrt(x^2 + y^2) and sqrt(x^2 + y^2 + z^2) respectively. If you write 'Vector3D a = Vector3D(5, 10, 15); Vector2D b = a;', what should 'a.Magnitude == b.Magnitude' return? If we then write 'a = (Vector3D)b', does a.Magnitude have the same value before the assignment as it does after? The .NET designers probably said to themselves, "no, we'll have none of that".
Juliet
Just because a problem can be solved, doesn't mean it should be solved. Sometimes it's just best to avoid situations where the problem arises.
Dan Diplo
+2  A: 

This seems like a very frequent question. I feel like adding that value types are stored "in place" where you declare the variable; apart from implementation details, this means that there is no object header that says something about the object, only the variable knows what kind of data resides there.

Cecil Has a Name
The compiler knows what's there. Referencing C++ this cannot be the answer.
Henk Holterman
Where did you infer C++ from? I'd go with saying in-place because that's what matches the behaviour the best, the stack is an implementation detail, to quote an MSDN blog article.
Cecil Has a Name
Yes, mentioning C++ was bad, just my train of thought. But aside from the question if runtime info is needed, why shouldn't structs have an 'object header' ? The compiler can mash them up anyway it likes. It could even hide a header on a [Structlayout] struct.
Henk Holterman
Because structs are value types, it is not necessary with an object header because the run-time always copies the content as for other value types (a constraint). It wouldn't make sense with a header, because that's what reference-type classes are for :P
Cecil Has a Name
+1  A: 

There is a point I would like to correct. Even though the reason structs cannot be inherited is because they live on the stack is the right one, it is at the same a half correct explanation. Structs, like any other value type can live in the stack. Because it will depend on where the variable is declared they will either live in the stack or in the heap. This will be when they are local variables or instance fields respectively.

In saying that, Cecil Has a Name nailed it correctly.

I would like to emphasize this, value types can live on the stack. This doesn't mean they always do so. Local variables, including method parameters, will. All others will not. Nevertheless, it still remains the reason they can't be inherited. :-)

Rui Craveiro
+7  A: 

I wanted to make this a comment but it's worth highlighting (and this is not an answer to the question, sorry)

One of the great mis-conceptions of .Net is that structs are allocated on the stack. Structs are not (always) allocated on the stack. Structs are value types, the lifetime of a value type is managed by it's 'container'. In the case of a value type declared as a local variable the container is the stack frame the value type is allocated on the stack and when the method returns the value is gone. In the case of an object the object is allocated on the heap, any value types contained by that object are also allocated on the heap as part of the objects memory and and that space gets reclaimed when the object is garbage collected.

Kevin Jones
Yes, glad to hear someone repeat this. +1
kek444
A: 

IL is a stack-based language, so calling a method with an argument goes something like this:

  1. Push the argument onto the stack
  2. Call the method.

When the method runs, it pops some bytes off the stack to get its argument. It knows exactly how many bytes to pop off because the argument is either a reference type pointer (always 4 bytes on 32-bit) or it is a value type for which the size is always known exactly.

If it is a reference type pointer then the method looks up the object in the heap and gets its type handle, which points to a method table which handles that particular method for that exact type. If it is a value type, then no lookup to a method table is necessary because value types do not support inheritance, so there is only one possible method/type combination.

If value types supported inheritance then there would be extra overhead in that the particular type of the struct would have to placed on the stack as well as its value, which would mean some sort of method table lookup for the particular concrete instance of the type. This would eliminate the speed and efficiency advantages of value types.

Matt Howells
C++ has solved that, read this answer for the real problem: http://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance/1223227#1223227
Dykam
+1  A: 

Structs do support interfaces, so you can do some polymorphic things that way.

Wyatt Barnett
+17  A: 

The reason value types can't support inheritance is because of arrays.

The problem is that, for performance and GC reasons, arrays of value types are stored "inline". For example, given 'new FooType[10]{...}', if FooType is a reference type, 11 instances will be created on the GC heap (one for the array, and 10 for each instance). If FooType is instead a value type, only one instance will be created on the GC heap -- the array itself (as each array value will be stored "inline" with the array).

Now, suppose we had inheritance with value types. When combined with the above "inline storage" behavior of arrays, Bad Things happen, as can be seen in C++: http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.4

Consider this pseudo-C# code:

struct Base {
  public int A;
}

struct Derived : Base {
  public int B;
}

void Square(Base[] values)
{
  for (int i = 0; i < values.Length; ++i)
    values [i].A *= 2;
}

Derived[] v = new Derived[2];
Square (v);

By normal conversion rules, a Derived[] is convertable to a Base[] (for better or worse), so if you s/struct/class/g for the above example, it'll compile and run as expected, with no problems. But if Base and Derived are value types, and arrays store values inline, then we have a problem.

We have a problem because Square() doesn't know anything about Derived, it'll use only pointer arithmetic to access each element of the array, incrementing by a constant amount (sizeof(A)). The assembly would be vaguely like:

for (int i = 0; i < values.Length; ++i) {
    A* value = (A*) (((char*) values) + i*sizeof(A));
    value->A *= 2;
}

(Yes, that's abominable assembly, but the point is that we'll increment through the array at known compile-time constants, without any knowledge that a derived type is being used.)

So, if this actually happened, we'd have memory corruption issues. Specifically, within Square() values[1].A*=2 would actually be modifying values[0].B!

Try to debug THAT!

jonp
The sensible solution to that problem would be to disallow the cast form Base[] to Detived[]. Just like casting from short[] to int[] is forbidden, although casting from short to int is possible.
nikie
+answer: the problem with inheritance didn't click with me until you put it in terms of arrays. Another user stated that this problem could be mitigated by "slicing" structs to the appropriate size, but I see slicing as being the cause of more problems than it solves.
Juliet
Yes, but that "makes sense" because array conversions are for implicit conversions, not explicit conversions. short to int is possible, but requires a cast, so it's sensible that short[] can't be converted to int[] (short of conversion code, like 'a.Select(x => (int) x).ToArray()').If the runtime disallowed the cast from Base to Derived, it would be a "wart", as that IS allowed for reference types. So we have two different "warts" possible -- forbid struct inheritance or forbid conversions of arrays-of-derived to arrays-of-base.
jonp
At least by preventing struct inheritance we have a separate keyword and can more easily say "structs are special", as opposed to having a "random" limitation in something that works for one set of things (classes) but not for another (structs). I imagine that the struct limitation is far easier to explain ("they're different!").
jonp
@jonp: short to int doesn't require a cast. It's an implicit conversion.
nikie
You're right, that is terrible assembly. In fact, I think you'd need a C compiler to compile it ;)
Richard Szalay
need to change the name of the function from 'square' to 'double'
John
A: 

Structs are allocated on the stack. This means the value semantics are pretty much free, and accessing struct members is very cheap. This doesn't prevent polymorphism.

You could have each struct start with a pointer to its virtual function table. This would be a performance issue (every struct would be at least the size of a pointer), but it's doable. This would allow virtual functions.

What about adding fields?

Well, when you allocate a struct on the stack, you allocate a certain amount of space. The required space is determined at compile time (whether ahead of time or when JITting). If you add fields and then assign to a base type:

struct A
{
    public int Integer1;
}

struct B : A
{
    public int Integer2;
}

A a = new B();

This will overwrite some unknown part of the stack.

The alternative is for the runtime to prevent this by only writing sizeof(A) bytes to any A variable.

What happens if B overrides a method in A and references its Integer2 field? Either the runtime throws a MemberAccessException, or the method instead accesses some random data on the stack. Neither of these is permissible.

It's perfectly safe to have struct inheritance, so long as you don't use structs polymorphically, or so long as you don't add fields when inheriting. But these aren't terribly useful.

Isn't this pretty much what has already been said? -1.
John Saunders
Almost. Nobody else mentioned the slicing problem in reference to the stack, only in reference to arrays. And nobody else mentioned the available solutions.