Tomas Petricek

Searching for new ways of thinking in programming & working with data

I believe that the most interesting work is not the one solving hard problems, but the one changing how we think about the world. I follow this belief in my work on data science tools, functional programming and F# teaching, in my programming languages research and I try to understand it through philosophy of science.

The Gamma

I'm working on making data-driven storytelling easier, more open and reproducible at the Alan Turing Institute.

Consulting

I'm author of definitive F# books and open-source libraries. I offer my F# training and consulting services as part of fsharpWorks.

Academic

I published papers about theory of context-aware programming languages, type providers, but also philosophy of science.

Tomas Petricek
  • Tomas Petricek
  • Home
  • F# Trainings
  • Talks and books
  • The Gamma
  • Academic

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

  • [1] Comega project home [^] - Microsoft Research

Published: Tuesday, 23 January 2007, 11:54 PM
Author: Tomas Petricek
Typos: Send me a pull request!
Tags: c#

Contact & about

This site is hosted on GitHub and is generated using F# Formatting and DotLiquid. For more info, see the website source on GitHub.

Please submit issues & corrections on GitHub. Use pull requests for minor corrections only.

  • Twitter: @tomaspetricek
  • GitHub: @tpetricek
  • Email me: tomas@tomasp.net

Blog archives

October 2020 (1),  July 2020 (1),  April 2020 (2),  December 2019 (1),  February 2019 (1),  November 2018 (1),  October 2018 (1),  May 2018 (1),  September 2017 (1),  June 2017 (1),  April 2017 (1),  March 2017 (2),  January 2017 (1),  October 2016 (1),  September 2016 (2),  August 2016 (1),  July 2016 (1),  May 2016 (2),  April 2016 (1),  December 2015 (2),  November 2015 (1),  September 2015 (3),  July 2015 (1),  June 2015 (1),  May 2015 (2),  April 2015 (3),  March 2015 (2),  February 2015 (1),  January 2015 (2),  December 2014 (1),  May 2014 (3),  April 2014 (2),  March 2014 (1),  January 2014 (2),  December 2013 (1),  November 2013 (1),  October 2013 (1),  September 2013 (1),  August 2013 (2),  May 2013 (1),  April 2013 (1),  March 2013 (1),  February 2013 (1),  January 2013 (1),  December 2012 (2),  October 2012 (1),  August 2012 (3),  June 2012 (2),  April 2012 (1),  March 2012 (4),  February 2012 (5),  January 2012 (2),  November 2011 (5),  August 2011 (3),  July 2011 (2),  June 2011 (2),  May 2011 (2),  March 2011 (4),  December 2010 (1),  November 2010 (6),  October 2010 (6),  September 2010 (4),  July 2010 (3),  June 2010 (2),  May 2010 (1),  February 2010 (2),  January 2010 (3),  December 2009 (3),  July 2009 (1),  June 2009 (3),  May 2009 (2),  April 2009 (1),  March 2009 (2),  February 2009 (1),  December 2008 (1),  November 2008 (5),  October 2008 (1),  September 2008 (1),  June 2008 (1),  March 2008 (3),  February 2008 (1),  December 2007 (2),  November 2007 (6),  October 2007 (1),  September 2007 (1),  August 2007 (1),  July 2007 (2),  April 2007 (2),  March 2007 (2),  February 2007 (3),  January 2007 (2),  November 2006 (1),  October 2006 (3),  August 2006 (2),  July 2006 (1),  June 2006 (3),  May 2006 (2),  April 2006 (2),  December 2005 (1),  July 2005 (4),  June 2005 (5),  May 2005 (1),  April 2005 (3),  March 2005 (3),  January 2005 (1),  December 2004 (3),  November 2004 (2), 

License

Unless explicitly mentioned, all articles on this site are licensed under Creative Commons Attribution Share Alike. All source code samples are licensed under the MIT License.

CC License logo