简体   繁体   中英

How do I get the return value while debugging?

I looked through SO but couldn't find the answer, I'm sure it's there though...?

While debugging, how do I get the value of the return statement if I put a breakpoint on it? I like to condense to a single line just so it looks "pretty". But I currently don't since I can't figure out how to debug the returned result...?

using (IUnitOfWork context = new EFUnitOfWork())
{
    var repo = new ReportRepository(context);
    return repo.GetProcedureReport(startDate, endDate).ToList();
    //return result.ToList();
}

In VS 2013, you can add the variable $ReturnValue to the watch. It contains the actual returnvalue from the function.

Select the method and right-click. Select Quickwatch from the menu.

在此输入图像描述

I'm assuming you cannot put a breakpoint within GetProcedureReport ?

The type of return value debugging you're attempting is simply not possible with managed languages like C#. The C++ debugger provides this information, via the autos window, but not managed languages.

The primary reason why is that the CLR debugging engine simply doesn't provide this value. For C#, VB or F# to provide this they would need to rewrite every return statement to spill the value into a temporary and then return the temporary. Return value debugging could then be achieved by surfacing this temporary in the debugger.

var returnTemp = repo.GetProcedureReport(startDate, endDate).ToList();
return returnTemp;

This would work but it would provide negatives to the code. Most notably that big struct values would end up being copied twice and contribute negatively to performance. Additionally this rewrite would need to occur at compile time and would affect every method being compiled. It would be much less impactful if it could be done on demand at debug time. The negatives just out weigh the benefits here.

Note that VB.Net does provide return value debugging to a small degree. I've blogged about how that works here

http://blogs.msdn.com/b/jaredpar/archive/2011/01/12/why-the-debugging-difference-between-c-and-vb-net-return-values.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM