Visual Studio debugger request: $return local variable

The previous post on the special $exception local variable was really just some background to ask for a new special local called $return that holds what is being returned by the current method (basically, the return value that’s on, or going to be, on the stack, but without having to use windbg or the like).  Certainly it would be nice if it also worked for arbitrary lambdas as well, but I’ll keep the scope of the request small for now Smile

So, here’s the hypothetical source:

Code Snippet
  1. using System;
  2. using System.IO;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Console.WriteLine(GetMyPath(null));
  9.         Console.WriteLine(GetMyPath("foo.txt"));
  10.     }
  11.  
  12.     private static string GetMyPath(string input)
  13.     {
  14.         if (input == null)
  15.         {
  16.             return "Invalid path";
  17.         }
  18.         return Path.GetFullPath(input);
  19.     }
  20. }

In this scenario, someone that’s debugging GetMyPath might want to see what’s being returned from the method.  Currently, they’re stuck assigning it to a local (or similar) inside the method (or putting the burden on all the callers to similarly assign it) in order to then see it, something like:

image

What some people may not realize is that you can set a breakpoint at the end of a method and it gets hit no matter the (normal) exit path.  For instance, for both of the callers in the above sample, they’ll hit such a breakpoint even though they exit via 2 different ‘return’ keywords.

image

image

Certainly there’s a school of thought (especially among old C++ developers) that you should create your return value at the top of your method and then return it at the bottom of your method (no multiple-return, no exception throwing, etc – inspired mostly by the various memory/resource handling patterns like needing to free allocated memory) but since the debugger obviously knows what the method is returning anyway, it would be awfully nice if it could show it to us Smile

About these ads

5 thoughts on “Visual Studio debugger request: $return local variable

  1. Pingback: showing C# method return in debugger – VB.NET can do it! « James Manning's Blog

  2. Pingback: Seeing a function’s return value in the debugger | Real Coding!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s