views:

362

answers:

5

If struct inherits from the class, in other words, value type inherits from a reference type, where is memory allocated: heap or stack?

Thanks

Edit#1

If there is no inheritance allowed, then why is the following legal:

class Samplewhere T:class where U:struct, T

In the case above, parameter U is value type, and it derives from T -- reference type.

Thanks 2

+1  A: 

MSDN says,

There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class object. A struct can implement interfaces, and it does that exactly as classes do.

codemeit
just thought the source shall be of help to the OP - http://msdn.microsoft.com/en-us/library/aa288471(VS.71).aspx
Gishu
@Gishu, of course, thanks.
codemeit
Well, it doesn't answer my question with, more info in the edit#1
A: 

Structs cannot inherit from anything other than System.ValueType or System.Enum. There is no way a struct can inherit from a normal reference type. So unfortunately this question cannot be answered.

JaredPar
<pedant> Or System.Enum </pedant>
Jon Skeet
@jon, oh yes, forgot about that one. Thanks!
JaredPar
+2  A: 

All structs derive from the ValueType type implicitly. You cannot specify an explicit base type.
Refer to this MSDN tutorial on structs as posted by codemelt.

  • When you instantiate a struct e.g. as a local variable, they are allocated on the stack (better performance)
  • Classes may contain structs as members - in which case they are allocated on the heap.
Gishu
Look at edit. thx
Please keep in mind that allocating a struct on the stack does not necessarily imply better performance. It's only in certain circumstances that you'll get better performance using a struct and they exist solely for those circumstances.
Randolpho
doesn't answer the question... thanks for useful info though
Allocating a large object on the stack will necessitate many copies as the struct is passed from method to method. If your struct is particularly heavy, you may end up with *worse* performance.
Randolpho
@Sasha: It certainly answers the first question!
Jon Skeet
A: 

Sasha Wrote:

If there is no inheritance allowed, then why is the following legal:

class Samplewhere T:class where U:struct, T

In the case above, parameter U is value type, and it derives from T -- reference type

Although that's legal from a generic contract standpoint, you'll never get any useful code that uses that class to compile, because you'll never have a type other than T=System.Object that fulfills the U constraint. You might consider that a very minor bug in the implementation of generics in C#.

Randolpho
why is it legal from a generic contract standpoint? thx
Because Sample<object, double> is legal. Sample<object, anyvaluetype> is legal but that's it.
ShuggyCoUk
Oh and jon's point about references too
ShuggyCoUk
+3  A: 

Contrary to another answer, there are types beyond T=System.Object where this compiles:

class Samplewhere T:class where U:struct, T

The "T : class" constraint doesn't actually mean that T has to be a class. It means T has to be a reference type. That includes interfaces, and structs can implement interfaces. So, for example, T=IConvertible, U=System.Int32 works perfectly well.

I can't imagine this is a particularly common or useful constraint, but it's not quite as counterintuitive as it seems at first sight.

As to the more general point: as Obiwan Kenobi says, it all depends on your point of view. The CLI spec has quite a complicated explanation of this, where "derives from" and "inherits from" don't mean quite the same thing, IIRC. But no, you can't specify the base type of a value type - it's always either System.ValueType or System.Enum (which derives from System.ValueType) and that's picked on the basis of whether you're declaring a struct or an enum. It's somewhat confusing that both of these are, themselves, reference types...

Jon Skeet
Excellent point, sir. Clearly I hadn't thought it all the way out when I answered.
Randolpho
Let me read that again :)
Gishu