struct - what is it for?

前端 未结 10 2395
小鲜肉
小鲜肉 2020-12-17 23:42

I know something about struct type. But I can\'t understand: what is it for? when have I use it? Classes, simple value-types and enums - that\'s all that I need

10条回答
  •  余生分开走
    2020-12-18 00:16

    MSDN provdies a guide : Choosing Between Classes and Structures:

    Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

    Do not define a structure unless the type has all of the following characteristics:

    • It logically represents a single value, similar to primitive types (integer, double, > and so on).
    • It has an instance size smaller than 16 bytes.
    • It is immutable.
    • It will not have to be boxed frequently.

提交回复
热议问题