Why value-types are stored onto Stacks?

前端 未结 6 1726
一生所求
一生所求 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:40

    Eric Lippert discusses this here; firstly, it is incorrect that "value types are stored on the stack". They sometimes are, but not as:

    • fields on a class
    • captured variables
    • variables in an iterator block

    When they can be stored on the stack it is a convenient way of modelling their lifetime, but it isn't required to store them on the stack. You could write a compiler+CLI that doesn't have a stack, for example.

提交回复
热议问题