Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug an Individual C# Method in Visual Studio 2017?

Can I somehow run a single method in C# using Visual Studio 2017 / ReSharper in order to debug it? I am aware that I can:

  • Run the entire app and set a breakpoint (pain since I need to go through logic to get to method)
  • Copy the method to a console app
  • Run a unit test in debug mode on the method
  • Go to some online C# pad (but then I need to duplicate all the using statements)

All of these are time consuming and I am wondering if there is some alternative.

EDIT: In SQL Management Studio, I can highlight an individual query and run it. I want some way to debug C# methods like this (obviously I would need to provide params for this). I haven't heard of anything like that, but it doesn't hurt to ask.

like image 714
VSO Avatar asked Dec 22 '25 14:12

VSO


1 Answers

You can use the C# interactive window (it doesn't support debugging, but lets you quickly iterate on functions).

You can highlight chunks of code and then click on the Execute in Interactive menu item.

To use this effectively, you probably need to add some references and some using blocks to the interactive window.

To add a reference, you can use the #r command:

#r "System.Windows.Forms"
#r "C:\Full\Path\To\Your\Reference\Assembly.dll"

You can use using blocks from there using the normal syntax

using System.Windows.Forms

You can also declare and use objects within the scope of the interactive window:

var f = new Form();
f.ShowDialog();

If you ever need to cleanup your interactive window, you can use the reset command:

#reset
like image 58
John Koerner Avatar answered Dec 24 '25 04:12

John Koerner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!