2

I have the following statement in C#:

if (func1() || func2())
{
    ...

I have placed a breakpoint on the if statement. After stepping through, I can see that the execution enters the then part of the if statement. How can I tell which function caused the if statement to be satisfied (or if both functions returned true)? Is it possible to see the return value from a function without first modifying the code and assigning the return value to an interim variable?

Update:

Per this question: Can I find out the return value before returning while debugging in Visual Studio?

The Autos window shows the return values from the two functions. In my case, func1 returned false, so func2 was called, which also returned false.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • This extension will save your day https://www.oz-code.com/ – Irshad Ahmed Akhonzada Apr 06 '18 at 09:39
  • 2
    put a breakpoint at start of func2: if debugger hit it, it means func1 returned false (and func2 as well, if code later enter the else part) – Gian Paolo Apr 06 '18 at 09:40
  • 2
    If both the functions get executed that means first one returned false. 2nd function won't be executed if first one returns true – Chetan Apr 06 '18 at 09:42
  • If you can not modify the function code, you should add two temporary bool variables to store the return value of each function. Then you can check those. Do not be worried about performance or memory wasting. The JIT compiler is pretty good at finding dead code or useless variables and cutting them out. So in all likely hood it would still run this way when outside of a Debug Build. But as Chetan Ranpariya said, be aware of the short-circuiting properties of the OR. I think the Binary OR can get around that. – Christopher Apr 06 '18 at 09:46
  • 1
    @Subuncu *"or if both functions returned true"* That will **never** happen. If the first one returns true, the second one won't be called. `||` is a conditional OR, if you want to evaluate both you would need to use a `|`. – Manfred Radlwimmer Apr 06 '18 at 09:47
  • If you haveReSharper installed, it will show the return values of the functions that were called. However, this is not native to VS. – Matt Hogan-Jones Apr 06 '18 at 09:49
  • @ManfredRadlwimmer Good point! I feel pretty stupid now. – Sabuncu Apr 06 '18 at 09:50
  • @MattJones I have ReSharper but had it suspended, as it was causing a freeze of 10 seconds on each and every startup. – Sabuncu Apr 06 '18 at 09:51
  • @Christopher I am *already* doing that, as I have explained in my question. – Sabuncu Apr 06 '18 at 09:53
  • @Sabuncu: Nowhere in the question did you mention storing the return values of func1() or func2() into bool variables, then evaluating the bool variables in the the IF statement. – Christopher Apr 06 '18 at 10:14
  • @Christopher See last sentence: "Is it possible to see the return value from a function **without first modifying the code and assigning the return value to an interim variable?**" – Sabuncu Apr 06 '18 at 10:17

0 Answers0