简体   繁体   English

如何在 Visual Studio (2008) 中快速测试代码片段?

[英]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.来自 Python,我可以在其中启动 iPython 来测试我在 Visual Studio 中寻找的一小段代码。 Creating projects and classes just to test out a small idea just feels so cumbersome.创建项目和类只是为了测试一个小想法感觉很麻烦。

You might want to look at LINQPad . 您可能想看看LINQPad It's particularly well suited for LINQ of course, but it's fine for other snippets too. 当然,它特别适合LINQ,但也适用于其他片段。

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 :) 我个人只是在测试目录(c:\\ Users \\ Jon \\ Test)中使用简单的文本编辑器和Test.csTest.cs进行编译:)

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

Have a look at the Immediate Window in Visual Studio. 看看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. 若要显示立即窗口,请打开一个项目进行编辑,然后从“调试”菜单中选择“ Windows”,然后选择“立即”。

I use powershell for testing .net concepts. 我使用Powershell来测试.net概念。 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. 但是,如果您需要测试程序结构或复杂的概念,请设计易于测试的界面,以便在范围超过5分钟测试时轻松进行测试。

If you just want to test a call to a core library, you can use PowerShell for that.如果您只想测试对核心库的调用,您可以使用PowerShell

For example this C# code:例如这个 C# 代码:

Convert.ToDecimal("20,000")

Would be this PowerShell command:将是这个 PowerShell 命令:

[System.Convert]::ToDecimal("20,000")

Which would result in this output:这将导致此输出:

20000 20000

If you want to test out a small script, there's a dotnet CLI tool for that called dotnet-script .如果你想测试一个小脚本,有一个名为 dotnet-scriptdotnet CLI 工具 Install it by running this command: dotnet tool install -g dotnet-script .通过运行以下命令安装它: 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:完成此操作后,您可以将 C# 代码块保存到 .csx 文件并使用 dotnet CLI 执行它,如下所示:

dotnet script myscript.csx dotnet 脚本 myscript.csx

Using my original example, the csx file would look like this to get the same output:使用我的原始示例,csx 文件将如下所示以获得相同的输出:

var aDecimal = Convert.ToDecimal("20,000");
Console.WriteLine(aDecimal);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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