匿名类型的属性setter

编程入门 行业动态 更新时间:2024-10-15 08:22:48
本文介绍了匿名类型的属性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

本文发布于:2023-11-07 00:38:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   类型   setter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!