Heap versus Stack allocation implications (.NET)

前端 未结 7 1357
梦如初夏
梦如初夏 2020-12-02 16:03

From an SO answer1 about Heap and Stack, it raised me a question: Why it is important to know where the variables are allocated?

At anoth

7条回答
  •  -上瘾入骨i
    2020-12-02 16:30

    I think the simplest reason is that if it is in the Heap the the garbage collection needs to deal with the variable once it is no longer needed. When on a Stack, the variable is dismissed with whatever was using it, such as a method that instantiated it.

提交回复
热议问题