Why are structs stored on the stack while classes get stored on the heap(.NET)?

前端 未结 11 732
你的背包
你的背包 2020-12-01 03:05

I know that one of the differences between classes and structs is that struct instances get stored on stack and class instances(objects) are stored on the heap.

Sinc

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 03:44

    It's useful in practice to be able to allocate memory on the stack for some purposes, since those allocations are very fast.

    However, it's worth noting that there's no fundamental guarantee that all structs will be placed on the stack. Eric Lippert recently wrote an interesting blog entry on this topic.

提交回复
热议问题