匿名类型的属性setter属性、类型、setter

2023-09-07 03:43:12 作者:问的一瞬间。对爱上了瘾

为什么匿名类型不具有属性setter?

  VAR一个=新的{text =你好};
a.Text =世界; //错误
 

解决方案

匿名类型是不可变的设计。

匿名类型是为了保存值,并重新$ P $类型psents值不应该是可变的。

此外,这将让他们在字典中不可靠的,因为哈希code可以创建后修改。 许多LINQ方法使用字典和,尤其是延迟评估,LINQ具有可变类型可导致微妙神秘的错误。

Why do anonymous types not have property setters?

var a = new { Text = "Hello" };
a.Text = "World"; //error
destoon分类属性筛选图解

解决方案

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.