C# Wishlist: Methods returning Anonymous Types

Anonymous Types were introduced in .Net 3.5, and I believe the motivation was to support the Linq Select() method which implicitly creates new types based on existing types (i.e. database-based classes) in statements like

var productQuery = 
    from prod in products
    select new { prod.Color, prod.Price };

This statement creates a strongly-typed read-only variable (productQuery) and an anonymous type with the properties Color and Price, based on the Product definition generated by the database OR mapper.

However, as stated in the linked article, these types cannot be passed as parameters or declared as return types of methods or delegates, unless variables of these types are cast to object.

In my opinion, however, the restriction on return types could easily be remove if some requirements are met. Just imagine two methods like these:

public var GetProducts()
    var productQuery = 
        from prod in products
        select new { prod.Color, prod.Price };
    return productQuery.ToList();

public var GetProduct(int productId)
    var productQuery = 
        .Where(prod => prod.productId)
        .Select(prod => new { prod.Color, prod.Price })
    return productQuery;

As with the Linq examples, these examples are again shortcuts avoiding explicit type declarations. The result is still strictly typed, but some restrictions remain:

  • if more than 1 return statement occurs, all return statements need to declare the same properties of the return type
  • no return statement may explicitly return null
  • the accessibility level of the returned type is the same as that of the declaring method

The question of returning anonymous types is usually regarded a closely related to Tuples introduced in .Net 4.0. The major drawback of tuples, as I see it, is that the Tuple’s properties are named Item1, Item2, etc without any possibility to give them useful or sensible names, other than declaring your on class. The feature seems confusing and already raised some questions on SO.

Tuples and anonymous return types allow another food for thought: the request to have syntactic support for tuples just as sketched by Miguel de Icaza in Mono (I don’t know whether it is included in released code). His example in C# is

(user, password, host, port, path) = ParseUri (url);

calling a method

Tuple<string, string, string, int, string> ParseUri (string url);

Using anonymous types, the method might as well be declared as

var ParseUri(string url)
    return new { User = "...", Password = "...", 
        Host = "www.example.com", Port = 80, Path = "/..." };

and invoked by

var parsedUri = ParseUrl("http://www.example.com/");

One can easily see that these methods (tuples and anonymous types) could be used interchangeably.

Personally, I find the tuple notation using round parens (as in Perl, Python) untypical for C#, which uses curly braces { } for structured data.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: