February 27, 2021
Hot Topics:

The New Anonymous Types Feature in C# 3.0

  • By Vipul Patel
  • Send Email »
  • More Articles »

Delving Deep into the Code

To verify that the compiled assembly indeed creates a new class, start up ILDASM (located in C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin) and select the recently compiled assembly, AnonTypes.exe. Expand the tree-view, and you should see a view similar to Figure 1.

Click here for a larger image.

Figure 1. ILDASM Expanded Tree-view of AnonTypes

If you look carefully, ILDASM discloses information on how an anonymous type "<Projection>f__0" has been created. Along with the class, the private variables _Name (of the type string) and _Price (of the type 'int') have also been created. The Get and Set methods for both these variables also have been created, and they have the properties Name and Price.

Double-clicking any of the methods or variables reveals code that is very much like normal code. For example, when you click the Name property, you'll see the following code:

.property instance string Name()
   .get instance string AnonTypes.Program/
   .set instance void AnonTypes.Program/
}    // end of property '<Projection>f__0'::Name

Multiple Anonymous Types

If you have more than one anonymous type declared and their types are similar, the C# compiler is smart enough to detect that and produce only one class and create two instances of that class.

Suppose you have the following code (the newly added code is boldfaced):

using System;
using System.Query;
using System.Data.DLinq;
namespace AnonTypes
   class Program
      static void Main(string[] args)
         var p1 = new {Name = "A", Price = 3};
         var p2 = new {Name = "A", Price = 3};
         Console.WriteLine("Name = {0}\nPrice = {1}",
                           p1.Name, p1.Price);

When you compile this code and open the assembly with ILDASM, the structure will like Figure 2.

Click here for a larger image.

Figure 2. Multiple Anonymous Types with Similar Declarations

As you can see, because the declarations were similar, C# created only one anonymous class, thus optimizing the code. If the declarations were not similar, you would have two anonymous classes.

If you modify the above code to look like the following (the changed code is boldfaced):

new {Name = "A", Price = 3};
var p2 = new {Name = "A"};

The C# compiler will create two different anonymous types because the types are different. Figure 3 shows the ILDASM representation.

Click here for a larger image.

Figure 3. Multiple Anonymous Types with Different Declarations

As you can see, C# has created two classes, <Projection>f__0 and <Projection>f__1.

Anonymous Types Without Explicit Class Structure Declaration

Anonymous types provide a mechanism for declaring anonymous types in your code without explicitly declaring the class structure. Additionally, the C# compiler is smart enough to optimize the creation of an anonymous type if one with a similar structure already exists.

Download the Code

To download the accompanying source code for the example program, click here.

About the Author

Vipul Patel is a Microsoft MVP (two years in a row) in Visual C#. He currently works at Microsoft through Volt Information Sciences, specializing in C# and deployment issues. Reach Vipul at Vipul_d_patel@hotmail.com.

Page 2 of 2

This article was originally published on March 8, 2006

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date