Welcome to MSDN Blogs Sign in | Join | Help

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
Published Friday, June 13, 2008 2:10 PM by Sree_c
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# &raquo; C# Debugging Improvement for VS 2008 SP1- Part II A C One: What The World Is Saying About A C One

# With SP1, VS 2008 manage anonymous types with C# Debugger

Saturday, June 14, 2008 2:26 AM by Matthieu MEZIL

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

# Avec le SP1, VS 2008 gère les types anonymes dans le debugguer

Saturday, June 14, 2008 2:28 AM by Matthieu MEZIL

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

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Saturday, June 14, 2008 1:22 PM by Tom Kirby-Green

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.

# C# Debugging Improvement for VS 2008 SP1- Part II

Monday, June 16, 2008 11:40 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Monday, June 16, 2008 5:15 PM by Sree_c

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

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Monday, June 30, 2008 4:59 PM by Mark Brindle

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

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Tuesday, July 01, 2008 2:37 AM by Craig

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

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Wednesday, July 02, 2008 2:22 PM by Miguel Madero

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

:)

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Thursday, July 03, 2008 3:46 AM by Shtong

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

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Thursday, July 03, 2008 5:24 PM by BitCrazed

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.

# re: C# Debugging Improvement for VS 2008 SP1- Part II

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?

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Wednesday, July 23, 2008 7:38 AM by Pete Hurst

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.

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Friday, July 25, 2008 6:44 PM by David Nelson

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

# re: C# Debugging Improvement for VS 2008 SP1- Part II

Thursday, August 07, 2008 12:07 PM by Carl

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?

# Ultracet.

Monday, August 25, 2008 1:52 PM by Ultracet.

Difference between percocet and ultracet. Ultracet.

# The Art of Debugging – A Developer’s Best Friend

Friday, February 06, 2009 11:50 PM by Cloud Teacher Dot Com

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

# The Art of Debugging – A Developer’s Best Friend

Sunday, February 08, 2009 2:57 AM by Cloud Teacher Dot Com

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

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker