本文介绍了匿名类型的属性setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么匿名类型不具有属性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解决方案
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.
更多推荐
匿名类型的属性setter
发布评论