What objects will be sent to Stack, and which heaps and why?

Possible duplicate:
How are managed memory descriptor types stored inside objects?

I have a class below

public class MyClass { public List<RequestRow> RequestRow { get; set; } public List<int> intList { get; set; } public string ErrorMessage { get; set; } public string SuccessMessage { get; set; } int i; DateTime dt = DataTime.Now; public void SomeMethod() { //some operation } } 

The question is what will go into the heap and what is the stack? And why?

So, MyClass, properties, fields and method will go somewhere or onto the stack?

I recently found this question in an interview and I am interested to know about it.

Edited

I liked Mr. John, and my doubts were resolved.

I have 2 more questions to ask ...

a) When int, I and DateTime dt will be considered as a value type and will be pushed onto the stack

b) If I have an interface, say

 namespace namespace1 { public interface IXLView { ExcelXP.Application ExcelApp { get; } ExcelXP.Workbook CurrentWorkBook { get; } ExcelVersion ExcelVersion { get; } } public enum ExcelVersion { Excel2003, Excel2007 } } 

, then where will the objects be placed in this case .. Stack or Heap? Thanks

+4
source share
4 answers

Which will go in a heap and what will add up? And why?

Everything goes to heap. What for? Since the heap is intended for data storage, where the storage lifetime cannot be determined in advance . In your example, the lifetime of everything — numbers, lines, lists — cannot be known in advance, so they should all go in a heap so that the garbage collector can determine when this storage is dead.

When will int me and DateTime dt be considered as a value type and pushed onto the stack?

They will always be considered value types, as they are value types. That is, they will be copied by value and will contain their own values.

They will never be pushed onto the stack because the lifetime of their vaults cannot be known in advance.

If I have an interface, then where will the objects be placed in this case ... a stack or a bunch?

The question makes no sense. You have only types, no objects. What subjects are you talking about?

But the answer is the same: objects will be pushed onto the stack if the lifetime of their storage is known in advance; otherwise, they will be placed in a garbage heap.

+8
source

All your variables are members of the reference type ( MyClass ), so they will all be on the heap ... for now.

However, as Eric Lippert loves, heap and stack are implementation details. See these blog posts:

+6
source

I think there is something wrong here, the code you wrote will not push any variables on the stack. The stack will contain only holding variables (not all types) in the local area or parameters, and not in calss elements.

0
source

I believe (please correct me if I am wrong) that structs / primitives (excluding strings) are allocated on the stack, and objects (including generics) are allocated on the heap.

-4
source

All Articles