Reputation:
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...
Upvotes: 9
Views: 4790
Reputation: 8895
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.
Upvotes: 0
Reputation: 101042
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
>
Upvotes: 2
Reputation: 5134
Another option for quick testing is Scratchpad.cs. Often it is better than creating command line projects just to try an idea.
Upvotes: 2
Reputation: 77546
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.
Upvotes: 3
Reputation: 29776
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.
Upvotes: 2
Reputation: 12349
I use linqpad http://www.linqpad.net/ for quick testing of c# code.
Upvotes: 8