这个问题在这里已经有了答案:




8年前关闭。






我写了这样的东西,

 var suspense = new { Name = "Android", Market = string.Empty };
 suspense.Market = "Potential";

.NET 抛出错误



我知道 C# 中的 AnonymousTypes 是不可变的,但为什么呢?这是由于 CLR 的某些限制吗?

最佳答案

插入不可变匿名类型的驱动因素是 ,因为 LINQ API 在内部使用哈希表,并且返回可以修改的匿名类型的投影是一种危险的情况。

您可以检查:

Immutable types: understand their benefits and use them

Anonymous Types and Object Identities By Tim Ng on MSDN

关于c# - 为什么匿名类型在 C# 中是不可变的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14336669/

10-12 05:11