When should I use a struct instead of a class?

后端 未结 15 2302
臣服心动
臣服心动 2020-11-22 13:00

MSDN says that you should use structs when you need lightweight objects. Are there any other scenarios when a struct is preferable over a class?

Some people might ha

15条回答
  •  旧巷少年郎
    2020-11-22 13:19

    Use a struct when you want value-type semantics instead of reference-type. Structs are copy-by-value so be careful!

    Also see previous questions, e.g.

    What's the difference between struct and class in .NET?

提交回复
热议问题