Can't return anonymous type from method? Really?

One of the new features introduced in C# 3.0 which will be available in Visual Studio "Orcas" (currently in CTP version) is anonymous type. Anonymous type is something very similar to tuple type from Cω [1] (which is based on tuple types known from many functional programming languages including F#). Anonymous types are extremely useful in LINQ queries, because it allows you to construct type with several properties without declaring the type (with all the properties). Example of query with anonymous type looks like this:

var q = from c in db.Customers
        where c.Country = "Czech Republic"
        select new { FullName=c.Name+" "+c.Surname, Address=c.Address };

Ok, it's probabbly not the best example, but it demonstrates the point - you want to return some information from query and you don't need to declare type that contains FullName and Address properties before, because you need it only for this single query (and you want to return only these two fields, so you don't transfer additional data that you don't need from database).

Now let's get to the second point - because anonymous types are really anonymous, you can't use them as return types from methods. This is intentional limitation of C# 3 - if you want to return it from method it should have some name to make the code more readable. In some sitations you can return anonymous type as an object and access it's properties using reflection, but this will be slow (so it's usually better to refactor the code and create named type).

How is this implemented?

Internally, the runtime still needs to know the type - because everything stays strongly typed. Therefore the compiler generates type to represent the anonymous type and gives it some unique name:

// Sample anonymous type..
var ann = new { City="Prague", Name="Tomas" };

Signature of the class generated by C# compiler looks like this:

[CompilerGenerated]
public sealed class <Projection>f__0
{
  // Methods
  public <Projection>f__0();
  public override bool Equals(object);
  public override int GetHashCode();
  public override string ToString();

  // Properties
  public string City { get; set; }
  public string Name { get; set; }

  // Fields
  private string _City;
  private string _Name;
}

Casting to anonymous types

There is one interesting quote in the C# 3.0 specification: "Within the same program, two anonymous object initializers that specify a sequence of properties of the same names and types in the same order will produce instances of the same anonymous type". This implies that when you use two anonymous types with same properties in two different places of same program, the compiler will use same generated class for these two types (this actually isn't true for the May CTP, but in the latest "Orcas" CTP it works as described).

This is interesting fact, because thanks to this it is possible to write the following "little trick" that makes it possible to return instance of anonymous type from method and cast it back to the original type (so you can directly access to properties of the object instead of using reflection). For the casting we need to know the type, but because the type is anonymous it's not that simple, however thanks to the type inference that is used when calling methods you can get the type without writing the actuall type name - you'll just need another variable (or expression) of the same type:

// Method that returns anonymous type as object
object ReturnAnonymous()
{
  return new { City="Prague", Name="Tomas" };
}

// Application entry-point
void Main()
{
  // Get instance of anonymous type with 'City' and 'Name' properties
  object o = ReturnAnonymous();
  
  // This call to 'Cast' method converts first parameter (object) to the
  // same type as the type of second parameter - which is in this case 
  // anonymous type with 'City' and 'Name' properties
  var typed = Cast(o, new { City="", Name="" });
  Console.WriteLine("Name={0}, City={1}", typed.Name, typed.City);
}

// Cast method - thanks to type inference when calling methods it 
// is possible to cast object to type without knowing the type name
T Cast<T>(object obj, T type)
{
  return (T)obj;
}

This trick uses type inference algorithm that is used when calling methods. If you create anonymous type with the same properties in second class, the compiler knows the type and can use it as type parameter that is passed to the Cast method. This assigns name of the anonymous type to the generic parameter T and you can cast the value (passed as a first parameter) back to the anonymous type.

So you can return anonymous types from method and use them in another method without reflection (the only overhead is one method call, one created object instance and one type cast). The question is whether you should do this?

No warranties!

Although this code works quite well (in latest "Orcas" CTP) it is quite risky to use it - first this behavior might change in the final version. Even if this will still work in final version, you can use it only for types in one assembly (two anonymous types from two different assemblies will be internally compiled to two different types that can't be converted using this trick). It also bypasses C# 3.0 limitation that has quite good reason - when you're returning object instead of named type it will make the code less readable and it will be difficult to understand for someone else. For me, it is more an interesting trick than something I'd like to see in my code :-), but if you know of any good reasons for using it, please let me know!

Links and references

Published: January 23, 2007 23:54
Tags: C# and LINQ




Share a link: Facebook


  • by Mads Torgersen (1/24/2007 5:24:47 AM)

    Hahaa, this didn't stay a secret for long! We refer to this trick as "cast by example". For anonymous types it does indeed hinge on the fact that we share the underlying class for a whole "program" in spec speak, which in practice means per assembly. So you still can't pass anonymous types between assemblies and rediscover their type. As you say, the behavior may change in the final version, or the behavior may stay, but the guarantee in the spec may go away. So hurry up and have fun with it while you can!

    Mads Torgersen, PM for the C# Language

  • What about dynamic interfaces? by Jonathan Allen (1/29/2007 6:02:30 AM)

    Can't C# just use dynamic interfaces like VB to specify the return type?

  • Re: What about dynamic interfaces? by Tomas (2/7/2007 12:17:52 AM)

    Hi Jonathan, although dynamic interfaces (like Duck typing in VB 9) isn't directly supported in C# it is not that difficult to implement this for C# as well. It has indeed less overheads than reflection (if interface implementations are cached). Dynamic interfaces are very intersting but in this scenario it is better to write standard class :-). More interesting scenario for dynamic interface implementations is for interoperability between dynamic and static languages (like using PHP/Phalanger class in C#.. I'll write article about this soon...)

  • RE: Can't return anonymous type from method? Really? by (6/20/2007 6:43:49 PM)

    Cany one cast between an anonymous class and a non-anonymous class with the same interface? I guess not.

    Would be nice to be able to construct explicit class objects from anonymous type objects, e.g.:

    class Person { public String name; public int age; }

    var p = new { name="fred", age=34 }
    Person pers = new Person( p );

  • RE: Can't return anonymous type from method? Really? by (7/19/2007 10:35:58 PM)

    You can simplify example, if you combine ReturnAnonymous() and Cast() into one method:
    T ReturnAnonymous<T>(T type)
    {
    return (T)(object) new { City="Prague", Name="Tomas" };
    }

  • RE: Can't return anonymous type from method? Really? by Tomas (7/30/2007 2:52:11 AM)

    >> Dynamic Interfaces
    Hm, I think that dynamic interfaces are a feature implemented by the VB compiler and not let's say as a library function that could be used from C#.

    Thing like this could be implemented for C# and there is an article about it:
    http://www.codeproject.com/cs/library/nduck.asp.
    The article acutally uses CodeDOM which isn't very efficient, but it could be done with Reflection.Emit (which is what we used in a Duck Typing for Phalanger that I wrote about some time ago).

  • RE: Can't return anonymous type from method? Really? by kane (10/3/2007 3:35:32 PM)

    Nifty! But I am really a bit wary and worry how these new features improve the language as a whole.

  • RE: Can't return anonymous type from method? Really? by Tomas (10/7/2007 2:14:01 AM)

    >> Kane
    I agree that anonymous types are arguable. I would much more prefer something like "anonymous types" from COmega where you can always use the type statically, because you can write something like:

    {int Age; string Name} Method()
    {
    // ...
    }

  • RE: Can't return anonymous type from method? Really? by William (10/17/2007 3:16:22 AM)

    Seems the issue could be resolved with the introduction of "AS", such as:

    var v = new {Name="", Age=0} as MyType
    var v2 = Change(v)

    void MyType Change(MyType mt)
    {
    mt.Name = "Bill";
    mt.Age = 1;
    return mt;
    }

    In this case MyType can only be "declared" the same way over the whole assembly. So you can use "as MyType" in multiple projections with compile-time checking. However the compiler flags and error if MyType is not declared same way, so the first declaration "sets" the class. In this case, it is not really anonymous anymore, however it adds ability to strongly type projections and also ref them across assemblies.

  • RE: Can't return anonymous type from method? Really? by Mike (12/9/2007 2:56:57 PM)

    Old post but I just wanted to react.

    I'm not sure, but couldn't you use generics on the method that returns an object to specify the object you want to get?

    public object GetOne<T>(int id) { ... }

    The caller can specify in T what kind of object it wants the result to be, so it has some certainty that it fits its needs?

  • RE: Can't return anonymous type from method? Really? by Aneel (2/6/2008 11:35:01 AM)

    Why is the order of the properties in the Initializer important. I mean two anonymous instances created using Initializers with the same property names but different order do not refer to the same Anonymous type. Can you please clarify why....
    thanks

  • RE: Can't return anonymous type from method? Really? by (2/12/2008 5:40:22 PM)

  • RE: Can't return anonymous type from method? Really? by Jacob (3/13/2008 10:07:26 PM)

    Here's an interesting alternative: initializing an anonymous type from a dictionary. See: http://jacobcarpenter.wordpress.com/2008/03/13/dictionary-to-anonymous-type/

  • Principle applied to all objects... by Kris (2/26/2009 9:46:24 PM)

    This works:
    static class Extensions
    {
    public static T Cast<T>(this object obj, T type)
    {
    return ((T)obj);
    }
    }

    class Program
    {

    static void Main(string[] args)
    {
    TestAnonType (new { Test1 = 1, Test2 = "Testing" } as object);
    TestAnonType2(new { Test1 = 2, Test2 = "Testing" } as object);
    }

    private static void TestAnonType<T>(T o ) where T : class
    {
    var t = o.Cast(new { Test1 = 0, Test2 = string.Empty });
    Console.WriteLine(
    string.Format(
    "Test1: {0}\tTest2: {1}",
    t.Test1,
    t.Test2
    )
    );
    }

    private static void TestAnonType2<T>(T o) where T : class
    {
    var y = new { Test1 = 0, Test2 = string.Empty };
    var v = o.Cast(y);
    Console.WriteLine(
    string.Format(
    "Test1: {0}\tTest2: {1}",
    v.Test1,
    v.Test2
    )
    );
    }

    }

  • RE: Can't return anonymous type from method? Really? by Bogdan (10/22/2009 6:21:07 PM)

    "...but if you know of any good reasons for using it, please let me know!"

    there are always reasons, especially when it comes to programming. I googled a little trying to find a way to send anonymous variables, and the your example and Kris's are really nice

    my reason is that I needed to create a thread with more than one parameter argument, and I think it's overkill to create a class for ONE function call.

  • RE: Can't return anonymous type from method? Really? by ngm (1/19/2010 9:08:20 PM)

    Your trick is evil and should never be used... but if you're going to do it, you may as well avoid the extra object allocations. I think the lambda below will get pulled into a static method and will hopefully be optimized away once it hits the JIT:

    return Cast(obj, () => new {City="", Name=""});

    // Cast method - thanks to type inference when calling methods it
    // is possible to cast object to type without knowing the type name
    T Cast<T>(object obj, Func<T> type) {
    return (T)obj;
    }

  • RE: Can't return anonymous type from method? Really? by Tomas (1/19/2010 10:15:56 PM)

    ngm thanks for the suggestion - this could be a more optimal implementation of this dirty trick :-)

  • Anonymous types, extension methods and typed collections by 3J (1/22/2010 3:39:57 AM)

    There's a similar trick you can do with anonymous types and typed collections. Try this:

    public static IEnumerable<T> AsEmpty<T>(this IEnumerable<T> col)
    {
    yield break;
    }

    The point to this? Any collection of objects can now be fed into an extension function that creates a collection, including those you make up yourself. Want a HashSet<'a>? No problem; just make an extension method and it's done.

    Example:

    var values = Enumerable.Range(0, 10).Select(x => new { Down = x - 5, Up = x + 5 });

    var hashedValues = values.AsEmpty().ToHashSet();

    // Do some processing, adding values as necessary...

    hashedValues will now accept the anonymously typed object and implement hash semantics using the auto-generated code for the unnamed type.

  • RE: Can't return anonymous type from method? Really? by BlueRaja (3/16/2010 4:59:49 PM)

    I've opened a feature request on Microsoft Connect:
    https://connect.microsoft.com/VisualStudio/feedback/details/542278/ability-to-return-strongly-typed-anonymous-classes#

  • RE: Can't return anonymous type from method? Really? by theBoringCoder (3/31/2010 5:59:05 PM)

    Ability to return strongly typed anonymous classes? Really? Can anyone here say "oxymoron"?


Send comments to this article

Comments for this article have been disabled. You are welcome to send additional comments using email.