简体   繁体   English

有没有办法在C#中创建匿名结构?

[英]Is there a way to create anonymous structs in C#?

There doesn't seem to be any way as anonymous types derive from object. 匿名类型从对象派生,似乎没有任何方法。 But I thought I'd ask since much of the time we use anonymous types in simple query expressions to extract subsets of data to be used in those anonymous types we create. 但我想我会问,因为很多时候我们在简单查询表达式中使用匿名类型来提取要在我们创建的匿名类型中使用的数据子集。 It just seems to me they should be structs (value types) for greater memory efficiency vs. reference types. 在我看来,它们应该是结构(值类型),以提高内存效率与引用类型。

Thoughts? 思考?

没有没有支持的C#语法会生成匿名结构

UPDATE: C# 7 now has value-type tuples, which can be used in the same sorts of contexts that reference-type anonymous types are used in. 更新:C#7现在具有值类型元组,可以在使用引用类型匿名类型的相同类型的上下文中使用。


There seems to be this commonly-held idea that value types are "more efficient" than reference types. 似乎有这种普遍认为的价值类型比参考类型“更有效”。 This is entirely mythical; 这完全是神话般的; they are more efficient for some operations and less efficient for others. 它们对某些操作更有效,而对其他操作效率更低。

For example, large value types are less efficient compared to reference types if the unit of work you are concerned about is the "copy the value to a new location" work. 例如,如果您关注的工作单元是“将值复制到新位置”工作,则大值类型与引用类型相比效率较低。 A reference type copies a pointer-sized reference irrespective of the size of the referred data and therefore copies in a single highly optimized machine instruction. 引用类型复制指针大小的引用,而不管引用数据的大小,因此复制在单个高度优化的机器指令中。 A value type copies the size of the data every single time, which can be quite large and take multiple instructions. 值类型每次复制数据的大小,这可能非常大并且需要多个指令。

Regardless, anonymous types are solely a convenience feature. 无论如何,匿名类型仅仅是一种便利功能。 If you don't like their performance characteristics, you don't have to use them. 如果您不喜欢它们的性能特征,则不必使用它们。 You can define your own struct if you'd rather. 如果您愿意,可以定义自己的结构。

For many usage cases, anonymous types and tuples would be more efficient if implemented as public-field structs than as immutable classes; 对于许多用例,如果实现为公共字段结构而不是不可变类,则匿名类型和元组将更有效; whether a particular anonymous type or tuple would be more efficient as such a struct or as an immutable class is not particularly a function of its size, but rather a function of how it is used. 特定的匿名类型或元组是否会更高效,因为这样的结构或不可变类不是特别是它的大小的函数,而是它的使用方式的函数。 If a thing is never going to be boxed, the relative cost of using a struct versus a class will depend upon the size and upon how many times the thing would have to be copied (note that so-called "immutable" structs often have to get copied many more times than public-field structs). 如果一个东西永远不会被装箱,那么使用结构与类的相对成本将取决于大小以及必须复制多少次(注意所谓的“不可变”结构通常必须比公共字段结构复制的次数多很多次。 If a thing only gets copied once or twice after construction (common in many usage scenarios), a public-field struct of practically any size will perform better than could an immutable class. 如果一个东西在构造之后只被复制一次或两次(在许多使用场景中很常见),那么几乎任何大小的公共字段结构都将比不可变类表现得更好。 On the other hand, if a thing will frequently get coerced to a reference type, an immutable class will substantially outperform a struct, no matter how small . 另一方面,如果事物经常被强制转换为引用类型, 则不可变类将大大优于结构, 无论多小

The expense of coercing structure types to reference types, and the fact that some usage cases would require doing so frequently, means that even if for most temporary objects structs would be more efficient than immutable class objects, using structs for everything would be less efficient than using immutable class objects for everything. 将结构类型强制转换为引用类型的代价,以及某些使用情况需要经常执行的事实,意味着即使对于大多数临时对象结构而言,结构也比不可变类对象更有效,使用结构对于一切都不如为一切使用不可变的类对象。 Usage cases where structs would allow overall performance to be improved by 10% or more are probably too rare to justify the cost of having two different kinds of tuples and two different kinds of built-in anonymous types. 结构允许整体性能提高10%或更多的用例可能太少,无法证明拥有两种不同类型的元组和两种不同类型的内置匿名类型的成本。 Code which needs the performance advantages of open-field structs can easily enough define structure types for their exact purposes. 需要开放式结构的性能优势的代码可以很容易地为其确切目的定义结构类型。

Have you profiled your app and found anonymous types to be the slowest part of it? 你有没有想过你的应用程序,发现匿名类型是最慢的部分? If so, I suggest you manually create the needed structs and re-test to see if that fixed your problem. 如果是这样,我建议你手动创建所需的结构并重新测试,看看是否能解决问题。 Otherwise, I'd spend more time worrying about the business problem at hand and less time typing (like the feature allows). 否则,我会花更多的时间来担心手头的业务问题和更少的打字时间(如功能允许)。

I sometimes use anonymous objects: 我有时使用匿名对象:

var x = new 
{
    itm0 = new { key = "key0", val = 0},
    itm1 = new { key = "key1", val = 1},
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM