Share via


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

Case1:

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"});
System.Diagnostics.Debugger.Break(); 

private static object Func<T>(T obj)
{
       object o = obj;
       System.Diagnostics.Debugger.Break();
       return obj;
}

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

image

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

image

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

image

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

image

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

Comments

  • Anonymous
    June 13, 2008
    The comment has been removed

  • Anonymous
    June 13, 2008
    Sreekar Choudhary wrote a post about a new (VS 2008 SP1) C# debugging management, we can now use an anonymous

  • Anonymous
    June 13, 2008
    Comme l'explique Sreekar Choudhary avec le SP1 de VS 2008, on a maintenant la possibilité d'utiliser

  • Anonymous
    June 14, 2008
    When are we going to be able to easily see the return value of a function when the instruction cursor is on the return statement. For years we've been forced to declare a redundant local variable before returning if we want to see this value. What's needed is a 'virtual' local that just holds the return value.

  • Anonymous
    June 16, 2008
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    June 16, 2008
    Thanks for the feedback Tom. I can see how this will be very usefull. In unmanaged code i always look at register EAX for the address of the return, it would be good to have something equivalent of that when debugging C#. Let me think about it for sometime ....

  • Anonymous
    June 30, 2008
    Tom, I agree. I really miss this feature from Delphi. Delphi has an automatic variable called 'result' of the functions return type.

  • Anonymous
    June 30, 2008
    The comment has been removed

  • Anonymous
    July 02, 2008
    Enable lamdas, anonymous delegates and LINQ expressions in the Watch, Quick Watch and Immediate Window would be great. :)

  • Anonymous
    July 02, 2008
    I'd also like to add my vote to Tom's suggestion. This feature would be definitely appreciated.

  • Anonymous
    July 03, 2008
    TOM FOR PRESIDENT! :D I've recently been doing the first native code development in almost 8 years and forgot how much I love being able to check the return value (EAX) from a function prior to returning. PLEASE PLEASE PLEASE add this capability.

  • Anonymous
    July 08, 2008
    Go, Tom, go! I can't believe Anders omitted this as a requirement for any language compiling to .NET. How does the man sleep at night?

  • Anonymous
    July 22, 2008
    The comment has been removed

  • Anonymous
    July 25, 2008
    The comment has been removed

  • Anonymous
    August 07, 2008
    I also would like to be able to see the return value.  It was only after years of writing "catch(Exception ex)" just to be able to see the exception in the debugger (and ignoring the "unused variable ex" warnings) that I discovered the "$exception" magic watch symbol.  How about a "$returnvalue" watch symbol?

  • Anonymous
    August 25, 2008
    Difference between percocet and ultracet. Ultracet.

  • Anonymous
    February 06, 2009
    Art is the process or product of deliberately arranging elements in a way that appeals to the sense or

  • Anonymous
    February 07, 2009
    Art is the process or product of deliberately arranging elements in a way that appeals to the sense or