When should I use a struct rather than a class in C#?

前端 未结 28 3102
予麋鹿
予麋鹿 2020-11-21 11:55

When should you use struct and not class in C#? My conceptual model is that structs are used in times when the item is merely a collection of value types. A way to

28条回答
  •  误落风尘
    2020-11-21 12:06

    Use a struct when you want value semantics as opposed to reference semantics.

    Edit

    Not sure why folks are downvoting this but this is a valid point, and was made before the op clarified his question, and it is the most fundamental basic reason for a struct.

    If you need reference semantics you need a class not a struct.

提交回复
热议问题