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

前端 未结 28 3325
予麋鹿
予麋鹿 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 11:59

    My rule is

    1, Always use class;

    2, If there is any performance issue, I try to change some class to struct depending on the rules which @IAbstract mentioned, and then do a test to see if these changes can improve performance.

提交回复
热议问题