开发者

Anonymous type property setters

开发者 https://www.devze.com 2022-12-21 16:29 出处:网络
Why do anonymous types not have property setters? var a = new { Text = \"Hello\" }; a.Text = \"开发者_开发百科World\"; //error

Why do anonymous types not have property setters?

var a = new { Text = "Hello" };
a.Text = "开发者_开发百科World"; //error


Anonymous types are immutable by design.

Anonymous types are meant to hold values, and a type that represents a value should not be mutable.

Also, it would make them unreliable in a dictionary, as the hashcode could change after creation.
Many LINQ methods use Dictionaries, and, especially with delayed evaluation, LINQ with mutable types can lead to subtle mysterious bugs.

0

精彩评论

暂无评论...
验证码 换一张
取 消