Why value-types are stored onto Stacks?

前端 未结 6 1723
一生所求
一生所求 2020-11-28 06:59

Why does C# (.Net) prefer stack to store value types? What is the primary reason behind this design? Is it because read/write operations to the stack take better advantage o

6条回答
  •  温柔的废话
    2020-11-28 07:52

    Your statement isn't totally true. A better version: C# stores local variables on the stack.
    And this isn't special or new, (almost) all programming languages use the stack for local variables and method return addresses. There is support for this down to the hardware.

    Furthermore, Valuetypes can be local variables or fields inside reference types. So valuetypes are not always stored on the stack. A more helpful statement: referencetypes are never stored on the stack.

    So don't focus on the stack too muuch, it's an implementation detail. Learn about value- and reference-types.

提交回复
热议问题