Why is it possible to instantiate a struct without the new keyword?

前端 未结 7 1815
夕颜
夕颜 2020-12-07 15:29

Why are we not forced to instantiate a struct, like when using a class?

7条回答
  •  抹茶落季
    2020-12-07 15:39

    Additionally to what was posted: Note that a struct cannot have a parameterless constructor or have a initializer for any of its instance fields. The default value is having all value type fields set to their default value (ex. 0 for ints, false for bool, etc.) and all reference type fields to null.

    Secondly, a struct is initialized, for example by calling a constructor or using default().

提交回复
热议问题