开发者

Which objects will go to Stack and which to Heap and why? [duplicate]

This question already has answers here: Closed 12 years ago.

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 stri开发者_C百科ng 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


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.


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:

  • The stack is an implementation detail (part one)
  • The stack is an implementation detail (part two)
  • The truth about value types


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜