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?
An anonymous struct declaration is a declaration that declares neither a tag for the struct, nor an object or typedef name. Anonymous structs are not allowed in C++. The -features=extensions option allows the use of an anonymous struct declaration, but only as member of a union.
Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Since there is no names, direct objects(or variables) of them are not created and we use them in nested structure or unions. Definition is just like that of a normal union just without a name or tag.
A structure is a key word that create user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. How to create a structure? 'struct' keyword is used to create a structure.
An anonymous union is a union without a name. It cannot be followed by a declarator. An anonymous union is not a type; it defines an unnamed object. The member names of an anonymous union must be distinct from other names within the scope in which the union is declared.
No there is no supported C# syntax which will produce anonymous structs
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With