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

前端 未结 19 1611
一向
一向 2020-11-22 01:51

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

19条回答
  •  执念已碎
    2020-11-22 02:11

    As previously mentioned: Classes are reference type while Structs are value types with all the consequences.

    As a thumb of rule Framework Design Guidelines recommends using Structs instead of classes if:

    • It has an instance size under 16 bytes
    • It logically represents a single value, similar to primitive types (int, double, etc.)
    • It is immutable
    • It will not have to be boxed frequently

提交回复
热议问题