1

The compiler seems to always name anonymous types so that they contain "AnonymousType".

new { Name = "Sean"}.GetType().Name // Gives: <>f__AnonymousType0`1

Can this be relied upon?

1
  • The "always compiler" always does. The "never compiler" never does.
    – Tergiver
    May 23, 2012 at 16:44

3 Answers 3

3

No, you can't rely on that. C# specification doesn't specify the name of compiler generated types. So future versions of the compiler(or alternate implementations like mono) may choose different naming schemes.

2

The C# compiler today will indeed include the name AnonymousType as part of the generated name in IL.

This is strictly an implementation detail though. It's not specified by the language or guaranteed to true going forward to future versions of the language

2

The only mention of the type name of an anonymous type in the C# 4.0 specification is in 7.6.10.6;

The name of an anonymous type is automatically generated by the compiler and cannot be referenced in program text.

In other words, no guarantees are given or implied, the compiler is free to generate any name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.