This is a mirror of official site: http://jasper-net.blogspot.com/

Why are anonymous types generic?

| Tuesday, December 21, 2010
Suppose you use an anonymous type in C#:

var x = new { A = "hello", B = 123.456 };

Ever taken a look at what code is generated for that thing? If you crack open the assembly with ILDASM or some other tool, you'll see this mess in the top-level type definitions

.class '<>f__AnonymousType0`2'<'<A>j__TPar','<B>j__TPar'>

What the heck? Let's clean that up a bit. We've mangled the names so that you are guaranteed that you cannot possibly accidentally use this thing "as is" from C#. Turning the mangled names back into regular names, and giving you the declaration and some of the body of the class in C#, that would look like:

[CompilerGenerated]
internal sealed class Anon0<TA, TB>
{
   private readonly TA a;
   private readonly TB b;
   public TA A { get { return this.a; } }
   public TB B { get { return this.b; } }    
   public Anon0(TA a, TB b)
   { this.a = a; this.b = b; }
   // plus implementations of Equals, GetHashCode and ToString
}

And then at the usage site, that is compiled as:

var x = new Anon0<string, double>("hello", 123.456);

Again, what the heck? Why isn't this generated as something perfectly straightforward, like:

[CompilerGenerated]
internal sealed class Anon0
{
   private readonly string a;
   private readonly double b;
   public string A { get { return this.a; } }
   public double B { get { return this.b; } }    
   public Anon0(string a, double b)
   { this.a = a; this.b = b; }
   // plus implementations of Equals, GetHashCode and ToString
}

Good question. Consider the following.

Read more: Fabulous Adventures In Coding

Posted via email from .NET Info

0 comments: