C# Debugging Improvement for VS 2008 SP1- Part II

Anonymous Types

On Further review there are a few problems with anonymous-types, they all boil down to the fact the names given to these types are not valid C# type names ( so that users don't explicitly use them in code). But while debugging this is exactly the kind of thing that one wants to do, Consider the following cases


The anonymous type appears in a cast, like when an anonymous type is returned from a function or when its cast from System.Object to the actual type.

var obj = Func(new{ I = 10, J = "Sree"});

private static object Func<T>(T obj)
       object o = obj;
       return obj;

At the first breakpoint add to watch any member on o results in


At the second break point adding to watch any member on obj results in


Case 2:

The user wished to create a instance of the anonymous type during a debugging session or when the Anonymous Type is a type argument on a generic type and the user want to evaluate a static member, use it in a cast etc. 

SP1 Changes

The anonymous type names are no longer invalid in the Expression evaluator. Therefor

  1. C# Constructs involving Anonymous-types can actually be evaluated at runtime and
  2. Instances of Anonymous-types can actually be created during a debugging session.

Which means using the same example as above and trying it with SP1 produces


See how the anonymous type name is no longer hidden and is used in the cast to bind to the members.

As an added bonus while debugging you can now create instances of anonymous types on the fly and use them. Assign them to objects and check for pathological conditions etc


Hope you enjoy discovering and debugging anonymous types now, that they are much more useful in the debugger.

As always do let me know if there are others things you would like to see me improve or implement.

kick it on DotNetKicks.com