showing C# method return in debugger – VB.NET can do it!

Last time I mentioned that I’d like the ability for the C# debugger to show the return value from a method.  One thing that some threads on the subject in various places (including StackOverflow) seem to argue is that the CLR doesn’t support this, so the C# debugger can’t do it.

For instance, this response (which admittedly is coming up on 2 years old):

http://stackoverflow.com/questions/591086/vs-get-returned-value-in-c-code

Unfortunately cluttering your code with temporary variables in the only way in managed code (C# or VB). The CLR has no support for "managed return values" in the debugger and hence VS does not either.

Since it’s a very simple test to show this to be false (at least in 2010 / .NET 4), I figured I’d include it in a post.

For this exercise, I’m using Visual Basic 2010 Express and Visual C# 2010 Express so anyone else should be able to reproduce this (for free) themselves.

In trying to keep the example simple, I just picked a method that would return something that would obviously change from call to call and couldn’t be pre-calculated (so the value being shown couldn’t be something from a different/separate call).

The C# code:

using System;

class Program
{
    static void Main(string[] args)
    {
        int multiplier = new Random().Next(10);
        Foo(multiplier);
    }

    static long Foo(int multiplier)
    {
        return DateTime.Now.Ticks * multiplier;
    }
}

The VB.NET code:

Module Module1

    Sub Main()
        Dim Multipler As Integer = New Random().Next(10)
        Foo(Multipler)
    End Sub

    Function Foo(ByVal Multiplier As Integer)
        Return DateTime.Now.Ticks * Multiplier
    End Function


End Module

The key screen shot is setting a breakpoint on the ‘end function’ line and looking at the Locals window. 

With VB.NET, the function name shows up as an entry in ‘Locals’ and the debugger shows us the value it’s returning!

image

The C# debugger has no such support, though – at the same ‘end of method’ breakpoint, only the parameter passed in is shown:

image

So, clearly the CLR has enough support for the VB.NET debugger to support this feature, which would seem to be a pretty strong argument that the C# debugger certainly could implement this feature.

Advertisements

Dear Roadrunner

Seriously?

 

C:\Users\James>tracert 152.1.1.22

Tracing route to ns1.ncsu.edu [152.1.1.22]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.0.1
  2    10 ms    21 ms    11 ms  user-0c8hjg1.cable.mindspring.com [24.136.206.1]
  3    21 ms    21 ms    21 ms  ten13-0-0-307.rlghnca-rtr2.nc.rr.com [66.26.44.121]
  4    27 ms    28 ms    28 ms  ge-5-3-0.chrlncpop-rtr1.southeast.rr.com [24.93.64.2]
  5    27 ms    25 ms    28 ms  ten1-3.chrlncsa-p-rtr01.southeast.rr.com [24.93.73.57]
  6   456 ms   469 ms   461 ms  ten3-0-0.gnboncsg-pe-rtr01.southeast.rr.com [24.93.73.34]
  7   451 ms   452 ms   447 ms  ten3-0-0.gnboncsg-p-rtr01.southeast.rr.com [24.93.73.73]
  8   457 ms   479 ms   466 ms  ten3-0-0.rlghncrdc-pe-rtr01.southeast.rr.com [24.93.73.38]
  9   458 ms   462 ms   463 ms  por100.twcc.rlghnc-a-c2702.nc.rr.com [24.27.255.255]
10   463 ms   475 ms   470 ms  rrcs-96-10-0-254.se.biz.rr.com [96.10.0.254]
11   468 ms   467 ms   492 ms  chltcrs-gw-to-rtpcrs-gw.ncren.net [128.109.212.2]
12   464 ms   449 ms   457 ms  chlt7600-gw-pri-to-chltcrs-gw.ncren.net [128.109.9.10]
13   469 ms   477 ms   458 ms  ncsu-gw-2-to-chlt7600-gw.ncren.net [128.109.248.62]
14   459 ms   471 ms   462 ms  itcore-x-ncsugw2.ncstate.net [152.1.6.249]
15   473 ms     *      495 ms  vl2910-dc2-6509-2.ncstate.net [152.1.6.226]
16   481 ms   463 ms   465 ms  ns1.ncsu.edu [152.1.1.22]

Cupertino & Redmond

Certainly we’ve all heard the accusations that Windows copied Apple, so I had to chuckle to run across some Silverlight framework code that showed Windows was an exact copy of Apple Smile

 

Code Snippet
  1. // Type: System.Windows.Input.ModifierKeys
  2. // Assembly: System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
  3. // Assembly location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\System.Windows.dll
  4.  
  5. using System;
  6.  
  7. namespace System.Windows.Input
  8. {
  9.     [Flags]
  10.     public enum ModifierKeys
  11.     {
  12.         None = 0,
  13.         Alt = 1,
  14.         Control = 2,
  15.         Shift = 4,
  16.         Apple = 8,
  17.         Windows = Apple,
  18.     }
  19. }