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 (18)

  1. Sreekar Choudhary wrote a post about a new (VS 2008 SP1) C# debugging management, we can now use an anonymous

  2. Comme l’explique Sreekar Choudhary avec le SP1 de VS 2008, on a maintenant la possibilité d’utiliser

  3. Tom Kirby-Green says:

    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.

  4. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  5. Sree_c says:

    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 ….

  6. Mark Brindle says:

    Tom, I agree. I really miss this feature from Delphi. Delphi has an automatic variable called ‘result’ of the functions return type.

  7. Craig says:

    Another vote for Tom’s suggestion.

    Does any of the improvements to debugging translate into edit-and-continue functionality benefits?

    Something that always gets in the way for me is editing code inside an anonymous block only to see the purple line appear and my edit-and-continue session no longer be allowed to proceed…

  8. Miguel Madero says:

    Enable lamdas, anonymous delegates and LINQ expressions in the Watch, Quick Watch and Immediate Window would be great.

    🙂

  9. Shtong says:

    I’d also like to add my vote to Tom’s suggestion. This feature would be definitely appreciated.

  10. BitCrazed says:

    TOM FOR PRESIDENT! 😀

    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.

  11. Still likes Delphi, and glad it's not Borland's any more says:

    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?

  12. Pete Hurst says:

    Ways to see the return value of a function:

    1. Select the function call itself, right-click, quick watch.

    2. Select the whole return expression (whilst breakpointed on the return statement), right-click, quick watch.

    The lesson here is you can add a watch to an entire expression or function call, not just to individual variables.

    Let me give an example:

    String test = "hello";

    Whilst you’re breakpointed on the above line, what is the value of the test variable? Of course, it doesn’t have a value until you’ve stepped *past* that line.

    Why would you be able to see a return value *before* it has returned?

    Perhaps it could be implemented by having an extra "virtual" step point directly after the return statement, where the value would be visible just before it’s returned.

    However since I can add a watch to the whole return expression anyway, it’s not really necessary.

  13. David Nelson says:

    @Pete,

    That’s all well and good, but what if the method has side effects, and/or takes a long time to execute? Adding the entire method call to the watch window is not a valid option in that case.

    "Why would you be able to see a return value *before* it has returned?"

    The last step in any method call is to jump back to the calling location. At that point the return value must already be available, either on the stack or in a register depending on your implementation. So if you break at that point, i.e. just before the jump, there is no reason why you shouldn’t be able to see the return value of the method.

  14. Carl says:

    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?

  15. Ultracet. says:

    Difference between percocet and ultracet. Ultracet.

  16. Art is the process or product of deliberately arranging elements in a way that appeals to the sense or

  17. Art is the process or product of deliberately arranging elements in a way that appeals to the sense or