tags:

views:

1159

answers:

10

I wonder if there is something like a standalone Version of Visual Studios "Immediate Window"? Sometimes I just want to test some simple stuff, like "DateTime.Parse("blah")" to see if that works. But everytime i have to create a new console application, put in my code and test it.

The Immediate Window sadly only works when I am debugging something. Could PowerShell do that? Just open a CLI similar to what cmd.exe does, allowing me to execute some C# code?

+6  A: 

Well, this isn't a direct answer to your question, but you could look at this tool:

Also, if you want to see the IL produced, or similar, there is a tool that plugs into Reflector, called Snippy, based on the Snippy tool that Jon mentions in his own answer further down.

All of these are very nice to use.

Lasse V. Karlsen
+3  A: 

If you're using Mono, there's this:

CsharpRepl

Don Box hacked something very simple up a few years ago too.

mackenir
damn, you got me by 28 seconds! good show sir.
Barry Fandango
+5  A: 

The Mono project includes an interactive C# shell, this may be just what you're looking for.

http://www.mono-project.com/CsharpRepl

Barry Fandango
+2  A: 

Along the lines of lassevk's answer, I've got "Snippy". This was developed for C# in Depth, and the UI is pretty rubbish, but it works - and lets you write extra members (methods, nested classes etc) as well, e.g.

public void Foo()
{ 
    Console.WriteLine("Hello");
}
...
Foo();

(The ... is used to tell Snippy "everything under here belongs in Main".)

Jon Skeet
+2  A: 

I also find that SharpDevelop is so quick and lightweight that it is the easiest way to whip off a quick test project.

Rob Prouse
+8  A: 

Linqpad - I use it like this all the time. http://www.linqpad.net/

Don't be misled by the name - that just describes the original motivation for it, not its functionality.

Just recently he released a version with proper statement completion - that's a chargeable add-on (the core tool is free), but a minute amount of money and well worth it, I think.

Will Dean
+4  A: 

As you suggest, PowerShell can do what you want. For example, to test your DateTime.Parse, the following one liner will do the trick:

PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("Blah")
Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is a unknown word starting at index 0." At line:1 char:25 + [System.DateTime]::Parse( <<<< "Blah")

PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("1/2/3")

01 February 2003 00:00:00

Note that the above uses the current release of PowerShell (v1.0). The next version of PowerShell will allows you to intermingle C# with PowerShell scripts more directly. To whet your appetite, watch this 7 minute screencast "C# to PowerShell" by Doug Finke. Very impressive!

Dan Blanchard
+1  A: 

You may find the Object Test Bench useful. It's not very well known, but lets you create instances of classes, execute static methods and so on. It can be useful for discovering how to use unfamiliar APIs or for quick debugging of your own classes and methods, saving the creation of a test harness for simple checks.

You can find the MSDN documentation here:

http://msdn.microsoft.com/en-us/library/c3775d98%28VS.80%29.aspx

Dave R.
I found that a few days ago "by accident". It's actually quite useful, although not really what I am looking for, but still +1 because it is indeed a "Hidden Gem" of Visual Studio.
Michael Stum
+1  A: 

If you could wait a while.. it looks like there could be a C# equivalent of Ruby's irb in time for C# 4.0 Anders H. demonstrated an interactive console session where you could type in arbitrary C# code and see results in his 'Future of C#' piece at PDC 2008. You could even pop a WPF Window from it and then play with it via the console interface. Pretty cool.

Gishu
+1  A: 

Use LINQPad.

Name notwithstanding, it can execute any C# or VB code, from simple expressions to entire classes.

Plus, it can visualize entire object graphs in the results.

You can even add references to your own assemblies.

SLaks