where will the class be stored if it has a struct variable?

It is already clear that since the structs are value types in c#, they are stored on stack whereas a class object is stored on the heap (its reference, of-course stored on stack).

Warn: (Yes, this might not always be true. Thanks to @Jon for correction) But in most cases, yes!

But, what abouta class one of whose member is of type struct ? Now, how will the memory model be?

BTW, how do I check if some object resides in stack or heap ?

Okay. Some assumptions:

  1. This class is local inside a function.
  2. The struct is a member not a variable. (Thanks to the corrections.)

Upvotes: 10

Views: 1695

Answers (3)

Mahmoud Basha
Mahmoud Basha

Reputation: 41

Imagine we have a class Employee that has a data member called id which is a struct.... And imagine we are in a function called callMe, and inside it we created an instance from the class Employee. what gonna happen??

because it's a class reference type in heap a space for it will be allocated and an address of that space will be stored in the reference variable in the stack. but what is the size of this space?? good question so if it's struct either int(4 bytes which pre-defined by Microsoft) or long(8 bytes) or what ever structs you have inside the class the size of them will be allocated in the heap and the instance of the class gonna be referenced by the reference which is stored in the call stack in one of the stack frames and this instance which inside it the Struct all of them gonna be deleted when GC deletes the instance not when the function ends pay attention plz here when the function ends the reference variable which carry the address gonna be deleted cause function ended and it was stored in the call stack but the id Struct that in the class wasn't deleted why??

casue it's stored in the heap cause we defined it in the class Employee which is Reference Type and C# for the Reference Types allocates space for them in heap and because it's in heap then it's GC responsiblity to delete it.....

but what we inside the body of this function we created an int variable what gonne happen ??? then because it's a struct Value Type and the creation of it is inside the body of the function then the space for this struct will be allocated in call stack which mean in the stack in one of the stack frames and be deleted when the function end .....

that's it guys.... (:

Upvotes: 3

dcastro
dcastro

Reputation: 68660

It is already clear that since the structs are value types in c#, they are stored on stack whereas a class object is stored on the heap (its reference, of-course stored on stack).

That assumption is incorrect. Value types only go on the stack when they're local variables, and are not part of a closure/lambda/anonymous method. Even then, they may be put on the heap if the jitter decides to.

But, what abouta class one of whose variable is a struct type ? Now, how will the memory model be?

The rule above should answer your question: since a value type can only be stored on the stack if it's a local variable, then a class's field must go on the heap.

Everything you need to know about value types: http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

Upvotes: 3

Roy Dictus
Roy Dictus

Reputation: 33139

The class itself will be a reference type, so instances of it will be kept on the heap.

The property that is a struct is an integral part of the instance (i.e., of the object), and so will also be kept on the heap, just like the int and enum properties of that object.

Note: There will be no references to the struct property, just like there are no references to the int and enum properties.

Upvotes: 8

Related Questions