0

Possible Duplicate:
Why value-types are stored onto Stacks?

I understand about boxing / unboxing but my question is why .NET make the choice of store ie. an integer in stack ?

Its logical when you are (simple) passing parameters between procs / functions... but... why...is this generallly used in .NET in any case ?

Thanks a lot!

Community
  • 1
  • 1
boctulus
  • 404
  • 9
  • 15
  • That's not entirely true. Read [this](http://stackoverflow.com/questions/815354/why-are-structs-stored-on-the-stack-while-classes-get-stored-on-the-heap-net/815420#815420) and [this](http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx). – Oded Oct 05 '11 at 15:15
  • Why should .NET choose not to use the stack for a short-lived variable if it is available? – Anthony Pegram Oct 05 '11 at 15:16
  • 3
    The question is based on a completely false premise. The .NET team did *not* make that decision, and so asking why they did is pointless. Integers are not always stored on the stack. Sometimes they are stored on the stack, sometimes they are stored in registers, and sometimes they are stored on the heap, depending on the lifetime of the storage. – Eric Lippert Oct 05 '11 at 15:17
  • Thanks a lot to All and speccially to "Eric Lippert" to take time to answer to a novice :) – boctulus Oct 05 '11 at 19:53

2 Answers2

0

Not all integers are in stack.only integers which are local to functions or passed between functions are stored in stack.

If the integer is a part of class(ref type) then the integer is stored on the place where the ref types are stored(managed heap)

Ashley John
  • 2,379
  • 2
  • 21
  • 36
0

Because allocating heap objects for every integer and dereferencing the pointers all the time would be orders of magnitude too slow.

Joshua
  • 40,822
  • 8
  • 72
  • 132