简体   繁体   中英

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 :)

我通过控制台应用程序保留了一个“沙盒”项目来完成这种工作。

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM