Why do we need struct? (C#)

后端 未结 9 2046
盖世英雄少女心
盖世英雄少女心 2021-02-05 21:18

To use a struct, we need to instantiate the struct and use it just like a class. Then why don\'t we just create a class in the first place?

9条回答
  •  Happy的楠姐
    2021-02-05 21:26

    Structs have many different semantics to classes. The differences are many but the primary reasons for their existence are:

    1. They can be explicitly layed out in memmory
      • this allows certain interop scenarios
    2. They may be allocated on the stack
      • Making some sorts of high performance code possible in a much simpler fashion

提交回复
热议问题