Anonymous Types in the .NET Framework

An anonymous type is an unnamed type


July 01, 2008
URL:http://www.drdobbs.com/web-development/anonymous-types-in-the-net-framework/208801831


In Visual Studio 2008, the C# compiler supports some new and quite interesting features. Some of them are just a pinch of syntactic sugar to save the developer the burden of some repetitive and imperative tasks such as creating quite simple getters and setters for class properties. Some other features, instead, add new capabilities to the language and the .NET programming platform as a whole. In this regard, the ASP.NET platform is no exception. In this article, I briefly discuss anonymous types with the goal of delimiting clearly their realm and avoid misuse.

To start off, what's an "anonymous" type and how is it different from a regular "known" type? Quite simply, an anonymous type is an unnamed type. So the only difference with a regular known .NET type is merely in the name. For the Common Language Runtime (CLR), in fact, anonymous and named types are exactly the same entity. Anonymous types can be used in a variety of scenarios, but they have been introduced primarily to support LINQ queries throughout the .NET and ASP.NET platform.

Here's what an anonymous type looks like:

var person = new { 
    FirstName="Nancy", 
    LastName="Davolio", 
    Age=28 
};

Essentially, an anonymous type enables you to create an instance of an object without writing or importing a definition for the object type. At a quick look, it looks similar to JavaScript objects, in turn based on associative arrays; but it's only the first look.

In the .NET Framework, the language compiler generates a class on the fly. However, the class has no human-readable and usable name and it inherits directly from System.Object. The developer may get to know the name of the dynamically generated class using the GetType method, but this piece of information is hardly useful. For example, if invoke te method GetType on the person variable defined earlier, here's what you obtain:


<>f__AnonymousType0`3[System.String,System.String,System.Int32]

Is this a piece of information you reckon useful? That's why these types are referred to as anonymous types. You should note that the name of the anonymous type is not strictly related to value or properties names, but it entirely compiler generated. So it may be different from compilation to compilation. As you can see, this is an even better reason not to rely on the name of an anonymous type in your coding.

Just because there's no class definition, the list of members includes only the properties you specify when you declare the object. The type of these properties is inferred and is then reported in the hidden type name.

As mentioned, anonymous types exist mostly (only?) to serve LINQ queries, but is there some other scenario where they can be helpful? In reality, not many.

Anonymous types work well in local scenarios where you need a temporary object to contain logically-related values. Needless to say, if you have other needs such as fields or methods, then just stick to named types and forget about anonymous types. Anonymous types can't be used in any situation where a type is expected. For example, you can hardly pass an anonymous type as a named type is expected and there's no syntax that lets you specify an anonymous type. The best you can do is trying to use an anonymous type where an Object is expected.

void Foo(object o)
{ ... }

However, within the method you can't address any of the properties of the anonymous type. All that you can do is using reflection.

void Foo(object o)
{
    Type t = o.GetType();
    PropertyInfo pi = t.GetProperty("FirstName");
    string v = pi.GetValue(o, null) as string;
    Console.WriteLine(v);
    return;
}

In this case, it will work. But is it worth the effort? Using anonymous types across methods is strongly discouraged. Consider that two objects are of the same anonymous type only if they live in the same assembly, use the same properties with the same names and types and listed in the same order.

The C# and Visual Basic .NET compilers needed anonymous types as a foundation for their LINQ support. Besides this, avoid anonymous types.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.