简体   繁体   中英

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.


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. 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},
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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