匿名类型的属性setter [英] Anonymous type property setters

查看:157
本文介绍了匿名类型的属性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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆