17

I can remember that I could see the return value of a method when I debug a C++ code in Visual Studio 6.0. Now I am missing that feature at C# in Visual Studio 2010.
Where can I see the return value or is there a cause for not showing it?

Edit1: An example:

string GetFullName()
{
  return GetFirstName() + " " + GetLastName();
}

When I step (debugger) from row beginning with return to next line I would like to see the full name in a debug variable window.

brgerner
  • 4,287
  • 4
  • 23
  • 38

5 Answers5

11

This was added in Visual Studio 2013:

The return value(s) get displayed in the “Autos Windows” (Debug->Windows->Autos) and you can also use the pseudo variable “$ReturnValue” in the Watch and/or Immediate window to fetch the last function’s return value.

http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/27/seeing-function-return-values-in-the-debugger-in-visual-studio-2013.aspx

Alex Edelstein
  • 518
  • 5
  • 19
  • Would you mind describing the solutions in your answer? This would avoid navigating out of SO and it would give a static copy in case the link dies. – SandRock Feb 10 '16 at 11:07
  • 1
    You can do the same for exceptions using the `$exception` variable. Like how they're consistent in naming and case. Great job. –  May 04 '17 at 15:05
3

Simply when you debug the code, when return value has evaluated, do one of these :

1- type $ReturnValue in immediate window

2- watch $ReturnValue in watch window

enter image description here

pixparker
  • 2,903
  • 26
  • 23
3

See this: https://connect.microsoft.com/VisualStudio/feedback/details/555859/see-return-value-in-managed-code (and you can upvote it)

Also there is a comment there from a guy who developed a plugin for VS to support that.

Upd. Tried that plugin. Looks great for now.

Archeg
  • 8,364
  • 7
  • 43
  • 90
  • Thank you. I will try the plug-in. – brgerner Feb 21 '12 at 11:33
  • I tried the BugAid plug-in and it works. You need to set it to Full Mode for using the return view feature. It is a beta and the release is only free for open source projects and MVPs. – brgerner Feb 22 '12 at 09:11
  • I just read at BigAid homepage (http://www.bugaidsoftware.com/purchase/) that the comercial license is not for free although it is only a beta. So I have to deinstall it because I cannot find any hint to a trial version for comercial users. – brgerner Feb 22 '12 at 13:39
  • Yet another comment about BugAid license. The "About" dialog shows the sentence "BugAid is unlicensed and it is on day 1 out of 60 of its trial." So I think it is ok to use it as a trial for 60 days? I'm a little bit confused. – brgerner Feb 22 '12 at 17:45
  • 1
    You have downloaded and installed the app from the official site. I don't think you are doing anything illegal unless you are downloading some unofficial releases. – Archeg Feb 22 '12 at 17:54
  • Thank you. I downloaded it from http://www.bugaidsoftware.com/download. So I hope it is legal to use it the next 60 days. – brgerner Feb 22 '12 at 17:58
  • 3
    @brgerner Hi guys, I'm the co-creator of BugAid. BugAid has a free trial for 60 days, which you can use freely for any purpose (commercial or otherwise). I hope you'll find it useful! Sorry for the confusion - we'll try to make it clearer on our website that it's a totally free 60 day trial. – Omer Raviv Feb 22 '12 at 20:03
3

You can set a breakpoint on the line of the return statement with the method. Then right-click the breakpoint and select 'When Hit...'. In here you can select to display a message or run a macro. For this we'll print a message that will be shown in the Output Window. For example if you have following code:

public double ReturnValue()
{
    var x = 3;
    var y = 4;
    //x and y can of course be non-constant vars too
    return Calculate(x, y);
}

public static double Calculate(int x, int y)
{
    return x * x + y * y;
}

If you set a breakpoint on the line "return Calculate(x, y);", right-click it and choose 'When Hit...' and choose this as message:

Value is : {Calculate(x, y)}

In this case the Output Window will display:

Value is : 25.0

Hope this helps!

Update:

This also works for your example with the names, methods:

public string GetFirst()
{
    return "Bill";
}

public string GetLast()
{
    return "Gates";
}

public string GetFull()
{
    return GetFirst() + " " + GetLast();
}

Place the breakpoint on the return statement in the GetFull() method and do the same as before. The message-body now looks like this:

Fullname is: {GetFirst() + " " + GetLast()}

And the Output Window will show:

Fullname is: "Bill Gates"

Abbas
  • 14,186
  • 6
  • 41
  • 72
  • Thank you. Is this solution better than writing the call in Watch window? I guess enter the call in Watch window is faster. – brgerner Feb 21 '12 at 11:31
  • 1
    The problem with the Watch is that you have to put a breakpoint and while debugging select the method and choose 'add watch'. You can't right-click the method-call and choose 'add watch' as this gives an error in the Value of the watch. The 'setup' of my solution takes a bit longer but when debugging you have no more concerns. I just gave a solution for immediately viewing the return value of a method call as you asked... :) – Abbas Feb 21 '12 at 11:39
1

You can't see the return value see the similar related question

Can I find out the return value before returning while debugging in Visual Studio

You can store the result into string variable so it will list in debug window.

enter image description here

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine(p.GetFullName());
            Console.ReadLine();   
        }

        string GetFullName()
        {
            string result =GetFirstName() + " " + GetLastName();
            return result;
        }

        string GetFirstName()
        {
            string firstname = "vishwanath";
            return firstname;
        }

        string GetLastName()
        {
            string lastname = "Dalvi";
            return lastname;
        }
    }
Community
  • 1
  • 1
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
  • The additional variable is only an option for methods which are often debugged. – brgerner Feb 21 '12 at 11:29
  • I just found yet another related question in *Related* list of this page at right hand side: http://stackoverflow.com/questions/1704268/getting-a-methods-return-value-in-the-vs-debugger. It seems that SO search while entering question is not so good than *Related* list. I guess the ask-question-search does not care about tags field. – brgerner Feb 21 '12 at 12:32