9

I'm often in the need of debugging/testing my code or a small part of it.

One way to go is of course to run the application I am developing, or when developing a class library creating a small test application.

Another way is to create a unit test just for debugging purpose and run it in Visual Studio.

But what if when I don't want to write additional code (like disposable unit tests) and I don't want to start the whole application (takes some time to start and to navigate to the code I want to debug)?

Is there a way to run a small portion of code in Visual Stuio respectively interpret C# code?

EDIT

I know about LINQPad.

But sometimes I want to know e.g. how an Form looks like while running or if a component is able to talk to a database. So LINQPad does not suit my needs in those cases...

  • 1
    This can be tricky if the code is dependent on other code. For really small independent fragments, why not create a command-line project from scratch and copy-and-paste the code there? – Vlad Jun 14 '12 at 14:32
  • You can use the QuickWatch option in visual studio. If you want to test a few different statements in the same code put a breakpoint in the position and open quickwatch window. you can find the result of the statement by writing it on the quickwatch window. – Ozgur Dogus Jun 14 '12 at 14:33
  • It's a separate project ? or a portion in a whole project ? – crassr3cords Jun 14 '12 at 14:35
  • **PowerShell**; it is installed by default on all Windows OS's and it uses .NET natively. You can instantiate any C# object from PowerShell. `using System.IO.File;` is the same as `[System.IO.File]` for example – Kellen Stuart Aug 22 '18 at 21:56

6 Answers6

8

I use linqpad http://www.linqpad.net/ for quick testing of c# code.

Jake1164
  • 12,291
  • 6
  • 47
  • 64
  • I was just using it to do some serious string manipulation that would have taken an age to get to in my application - it is awsome! – MoonKnight Jun 14 '12 at 14:33
  • 1
    The free version doesn't include intellisense, but if you are copying code out of VS it shouldn't matter.. however its worth every penny for the full version :) (not affiliated, just a happy customer) – Jake1164 Jun 14 '12 at 14:34
  • Yeah, I know and use linqpad. But you can't e.g. show a `Windows.Form` for debugging purpose... –  Jun 14 '12 at 14:41
  • 2
    @Rhenser You actually can show a Form in LINQPad. You just need to add a refence to System.Windows.Form. Have a look [here](http://stackoverflow.com/questions/1222009/how-does-linqpad-reference-other-classes-e-g-books-in-the-linq-in-action-sampl) to see how to add references. Maybe that's your problem. – sloth Jun 14 '12 at 14:57
  • @dkson OK, I didn't know you could add refences to your code in LINQPad. Is it a new feature? It's actually a long time ago since I used LINQPad... –  Jun 14 '12 at 14:59
3

Another option is to download the latest Roslyn community preview, and have at it with the C# Interactive Window. It's pre-release software, so it doesn't support the entire C# feature set, but it's getting pretty close.

Vlad
  • 35,022
  • 6
  • 77
  • 199
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
2

Have a look at LINQPad. It's super for trying out snippets of code. Don't be put off by the name, it has support for C#/F# programs and expressions.

James World
  • 29,019
  • 9
  • 86
  • 120
2

Another option for quick testing is Scratchpad.cs. Often it is better than creating command line projects just to try an idea.

Pol
  • 5,064
  • 4
  • 32
  • 51
2

Have a look at the F# interpreter.

I use it regulary to test small code samples.

Let's say you have some code like this:

namespace ConsoleApplication5
{
    public class Test
    {
        public Int32 Sum(int a, int b)
        {
            return a + b;
        }
    }
    ...
}

Fire up the F# Interactive window, add a referece to your assemblies, and start debugging!

> #I @"C:\PathToYourProject\bin\debug";;
--> Added .... to library include path
> #r "ConsoleApplication5.exe";;
--> Referenced ...
> let t = ConsoleApplication5.Test();;
val t : ConsoleApplication5.Test
> t.Sum(9, 7);;
val it : int = 16
> 
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Ha, this works quite well! I did a quick test with a dynamically created/populated Form I am currently debugging, and it worked! –  Jun 14 '12 at 14:51
0

Actually believe it or not, PowerShell uses the .NET framework. You can call any built-in C# class from PowerShell.

Let's say you're trying to test the String.Substring method.

Open PowerShell

Then to call the function type this:

"some string".Substring(2,1) # returns 'm'

Or let's say you want to test a more complicated built-in class

foreach($line in [System.IO.File]::ReadLines("C:\path\to\file.txt"))
{
     $line
}

[System.IO.File] is the same thing as saying using System.IO.File;

The beautiful thing about PowerShell - it is C#'s console... and nobody knows it.

Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82