tags:

views:

151

answers:

4

Possible Duplicate:
How does .net managed memory handle value types inside objects?

I have a class as 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 Which will go to heap and which to stack? And why?

Means MyClass , the properties, fields, and the method will go where..heap or stack?

Recently I found this question in an interview and is curious to know about this

Edited

I liked Mr.John's answer and my doubts have been cleared..

I have 2 more questions to ask...

a) When will int i and DateTime dt be treated as Value type and will be put into 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 be the objects be placed in this case..Stack or Heap? Thanks

A: 

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

Richard J. Ross III
Yup, I'm afraid you're wrong. For example, `i` and `dt` here are value types, but they'll still be on the heap because they're part of a class.
Jon Skeet
A correct statement is that structs are allocated on the stack when they are local variables or temporaries that are not closed-over locals of an anonymous method or lambda expression and they are not in an iterator block. In all other cases they are allocated on the heap.
Eric Lippert
+5  A: 

All of your variables are members of a reference type (MyClass), so they'll all be on the heap... at the moment.

However, as Eric Lippert is fond of saying, the heap and stack are implementation details. See these blog posts:

Jon Skeet
@Fredrik: I was busy adding the links :)
Jon Skeet
Comment replaced with upvote :)
Fredrik Mörk
Sir, thanks for the clarification.. I have 2 more questions to ask... a) When will int i and DateTime dt be treated as Value type and will be put into stack (b) If I have an Interface say public interface IXLView { ExcelXP.Application ExcelApp { get; } ExcelXP.Workbook CurrentWorkBook { get; } ExcelVersion ExcelVersion { get; } } public enum ExcelVersion { Excel2003, Excel2007 } then where will be the objects be placed?
priyanka.sarkar_2
@priyanka... int and DateTime will always be treated as value types, because that's what they are. I think you're still relating value types to the stack. Read the blogs!
Anthony Pegram
A: 

I think something's wrong here, the code you wrote will not push any variables on the stack. The stack will hold only hold variables (not all types) in a local scope or parameters, not calss members.

Neowizard
can you please site a sample example for me to understand better
priyanka.sarkar_2
+5  A: 

Which will go to heap and which to stack? And why?

Everything there goes onto the heap. Why? Because the heap is for storage of data where the lifetime of the storage cannot be determined ahead of time. In your example the lifetime of everything - the numbers, the strings, the lists - cannot be known ahead of time, so they all have to go onto the heap so that the garbage collector can determine when that storage is dead.

When will int i and DateTime dt be treated as Value type and will be put into stack?

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

They will never be put onto the stack because the lifetimes of their storages cannot be known ahead of time.

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

The question doesn't make any sense. You've only got types, no objects. What objects are you talking about?

But the answer is the same: the objects will be put on the stack if the lifetimes of their storages are known ahead of time; otherwise, they'll be put on the heap to be garbage collected.

Eric Lippert