Freigeben über


How to view the return value of a C# or VB method in the VS Debugger

One of the questions that comes up frequently is how do I view the return value of a VB or C# method in the VS debugger? For example, let’s say that you have the following class:

    1:    public class Sphere
    2:    {
    3:      // Area = 4 * Area of Circle
    4:      public double CalculateArea(double radius)
    5:      {
    6:        Circle c = new Circle();
    7:        return (4 * c.CalculateArea(radius));
    8:      }
    9:    }

In the debugger, how can I see the value that is returned by c.CalculateArea(radius)? This comes in quite handy when you have methods that take other methods as their parameters, e.g. F(A(x), B(y)), where A() and B() are methods themselves. There are a number of ways of doing this in the debugger.

Using the Watch window

Let's say that you are stopped on the return statement of the Sphere.CalculateArea() method, as shown here:

 return (4 * c.CalculateArea(radius));

To view the return value of c.CalculateArea(radius), you can add the statement to the Watch window. Once you've added the statement to the watch window, you can Step Over (F10) the return statement and see the value in the Watch window. You can add the statement to the Watch window using one of the following ways:

  • Highlight c.CalculateArea(radius) in the editor window and drag it from the editor to the Watch window.
  • Type c.CalculateArea(radius) in the Watch window.
  • Highlight c.CalculateArea(radius) in the editor window, right-click it and select "Add Watch".

Here's a screen shot of the Watch window that shows this.

 Watch window

When you no longer need to view the return value of the method, you can simply delete it from the Watch window.

Using the Immediate window

Using the same scenario as above, you can also view the return value of a method using the Immediate window. To view the return value of c.CalculateArea(radius), type the statement in the Immediate window, do one of the following:

  • Type the statement in the Immediate window and press enter.
  • Highlight the statement in the editor window, drag and drop the entire statement from the editor to the Immediate window. Press enter.
  • Copy and paste the statement from the editor window to the Immediate window. Press enter.

Here's a screen shot of the Immediate window that shows this.

Immediate window

Using a temporary variable

You can also view the return value of a method by storing its return value in a temporary variable first. The downside of this approach is that you will need to modify your code to do this. From the example above, you can change the code as follows:

    1:  public class Sphere
    2:  {
    3:    // Area = 4 * Area of Circle
    4:    public double CalculateArea(double radius)
    5:    {
    6:      Circle c = new Circle();
    7:      double area = c.CalculateArea(radius);
    8:      return (4 * area);
    9:    }
   10:  }

Here, I've introduced a new temporary variable called area (line 7 above) that stores the return value of Circle.CalculateArea().

Using a breakpoint

If the method for which you want to see the return value is code that you have written and therefore you have access to the source code, you can view the return value by setting a breakpoint on the return statement of the method. Once the breakpoint is hit, you can inspect the value that is being returned. To continue with our example, since I have the source code for the Circle class, I can set a breakpoint on the return statement of its CalculateArea() method as shown below. Once you are stopped on the breakpoint, you can type in the return expression in the Watch window.

Breakpoint in edir

Comments