How can I quickly test a code snippet in Visual Studio (2008)?
Coming from Python where I can just fire up iPython to test out a small snippet of code I'm looking for the same in Visual Studio. Creating projects and classes just to test out a small idea 开发者_如何转开发just feels so cumbersome.
You might want to look at LINQPad. It's particularly well suited for LINQ of course, but it's fine for other snippets too.
Personally I just use a simple text editor and Test.cs
in a test directory (c:\Users\Jon\Test) and compile from the command line :)
I keep a "Sandbox" project with a console application to do this kind of stuff.
Have a look at the Immediate Window in Visual Studio.
The Immediate window is used at design time to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging.
To display the Immediate window, open a project for editing, then choose Windows from the Debug menu and select Immediate.
I use powershell for testing .net concepts. As it allows a more iterative learning process. If you need however to test program structure or complicated concepts designing easily testable interfaces easy to test as scope moves beyond the 5 minute test.
If you just want to test a call to a core library, you can use PowerShell for that.
For example this C# code:
Convert.ToDecimal("20,000")
Would be this PowerShell command:
[System.Convert]::ToDecimal("20,000")
Which would result in this output:
20000
If you want to test out a small script, there's a dotnet CLI tool for that called dotnet-script. Install it by running this command: dotnet tool install -g dotnet-script
.
Once you've done that, you can save a block of C# code to a .csx file and execute it using the dotnet CLI like this:
dotnet script myscript.csx
Using my original example, the csx file would look like this to get the same output:
var aDecimal = Convert.ToDecimal("20,000");
Console.WriteLine(aDecimal);
精彩评论