tags:

views:

82

answers:

3

How do I inspect the return value of this GetItems() function using the debugger? Do I have to create a local variable for the results to accomplish this?

foreach (string item in GetItems())
{
    // some code
}

private List<string> GetItems()
{
    // return some list
}
+7  A: 

You should be able to just add it as a Watch, and view the values as expected.

Debugging : The Watch Window

How to: Watch an Expression in the Debugger

astander
+2  A: 

No, you can add a watch or a quickwatch to GetItems() and you would see the result

Claudio Redi
A: 

You could to this:

var items = GetItems();

foreach (var item in items)
{
    // some code
}

Edit - in response to the comment, I agree with what astander is saying, but I actually prefer to not do "inline" method calls inside other constructs (method calls, if statements, loops, etc.). For example, if you have a method call that looks like this:

var result = SomeMethod(GetCode(), GetItems(), GetSomethingElse(), aVariable);

I think it's actually easier to read and debug if you do this instead:

var code = GetCode();
var items = GetItems();
var somethingElse = GetSomethingElse();

var result = SomeMethod(code, items, somethingElse, aVariable);

With the second way, you can more easily set a breakpoint on the method you actually want to step into, rather than having to step over other method calls before stepping into the method you want to debug. Just a personal preference.

Andy White
I have always felt that creating additional code for debugging purposes like this seems over the top. If the IDE does not support it, the IDE is lacking. THis is correct, but seems wrong when changing your code to debug like this...
astander