开发者

Inspecting values using the debugger in C#

How do I inspect the return value of this GetItems() function using the debugger? Do I have to create a local variable fo开发者_如何学编程r the results to accomplish this?

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

private List<string> GetItems()
{
    // return some list
}


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜