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

前端 未结 28 2653
予麋鹿
予麋鹿 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:01

    I rarely use a struct for things. But that's just me. It depends whether I need the object to be nullable or not.

    As stated in other answers, I use classes for real-world objects. I also have the mindset of structs are used for storing small amounts of data.

提交回复
热议问题