检查方法调用的返回值

在**“自动”**窗口中,当您单步跳过或单步跳出方法调用时,可以检查 .NET Framework 和 C++ 方法的返回值。 当方法调用的结果未存储在局部变量中时(例如,当方法用作另一个方法的参数或返回值时),此功能很有用。

内容

在“自动”窗口中查看方法返回值

在“即时”和“监视”窗口中查看 .NET Framework 方法返回值

在“自动”窗口中查看方法返回值

  1. 创建一个 C# 或 C++ 控制台应用程序。

  2. 将 C# Main 方法或 C++ _tmain 方法替换为以下代码。

    //static void Main(string[] args) {
        Method1();         // 1. Set a breakpoint here
                           // 2. Then step into Method1 
        int y = Method2(); // 3. Set a breakpoint here
                           // 4. Then step into Method2 
    
    static void Method1(){
        // 1. Step over the following line
        int result = Multiply(FourTimes(Five()), Six());
        // 2. Then view the return values in the Autos window
    }
    
    static int Method2(){
        // 1. Step over the following line
        return Five();
        // 2. Then view the return values in the Autos window
    }
    
    static int Multiply(int x, int y){
        return x * y;
    }
    
    static int FourTimes(int x){
        return 4 * x;
    }
    
    static int Five(){
        return 5;
    }
    
    static int Six(){
        return 6;
    }
    
    void Method1();
    int Method2();
    int Multiply(int x, int y);
    int FourTimes(int x);
    int Five();
    int Six();
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Method1();            // 1. Set a breakpoint here
                              // 2. Then step into Method1 
        int x = Method2();    // 3. Set a breakpoint here
                              // 4. Then step into Method2 
    }
    
    void Method1(){
        // 1. Step over the following line
        int result = Multiply(FourTimes(Five()), Six());
        // 2. Then view the return values in the Autos window
    }
    
    int Method2(){
        // 1. Step over the following line
        return Five();
        // 2. Then view the return values in the Autos window
    }
    
    int Multiply(int x, int y){
        return x * y;
    }
    
    int FourTimes(int x){
        return 4 * x;
    }
    
    int Five(){
        return Six();
    }
    
    int Six(){
        return 6;
    }
    
  3. 在 main 方法中的对 Method1 和 Method2 的调用处设置断点。

  4. 在**“调试”菜单中,选择“启动调试”**(键盘:F5)可开始调试并在 Method1 调用处中断。

  5. 选择**“调试”“逐语句”**(键盘:F10)以输入 Method1。

  6. 选择**“调试”“单步执行”**(键盘:F11)以单步执行 Method1 的第一行代码。

  7. 在**“自动”窗口中,您会发现显示了 Multiply、FourTimes、Five 和 Six 方法的返回值并包含返回值图标。(若要打开“自动”窗口,请选择“调试”“窗口”“自动”**或按 Ctrl + Alt + V, A)

    “自动”窗口中的方法返回值

  8. 选择**“调试”“继续”**(键盘:F5)以继续执行对 Method2 的调用。

  9. 单步执行 Method2。

  10. 逐过程执行 return 语句。

  11. 请注意,**“自动”窗口显示“五”**方法的返回值(由 Method2 直接返回的值)。

    “自动”窗口中的返回值

在“即时”和“监视”窗口中查看 .NET Framework 方法返回值

在单步执行或跳出方法调用后,您还可以通过在**“即时”窗口或监视窗口中键入 $ReturnValue 来检查 .NET Framework 方法调用的返回值。 若要打开“即时”窗口,请选择“调试”“窗口”“即时”**(键盘:Ctrl + Alt + I)。