Are value types immutable by definition?

后端 未结 12 1514
陌清茗
陌清茗 2020-12-04 09:18

I frequently read that structs should be immutable - aren\'t they by definition?

Do you consider int to be immutable?

int i         


        
12条回答
  •  鱼传尺愫
    2020-12-04 09:27

    aren't value types immutable by definition?

    No they're not: if you look at the System.Drawing.Point struct for example, it has a setter as well as a getter on its X property.

    However it may be true to say that all value types should be defined with immutable APIs.

提交回复
热议问题