如何快速编码和运行小的 C# 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/384743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:25:46  来源:igfitidea点击:

How to quickly code and run small C# code

c#visual-studiotesting

提问by Gant

There are times when I want to test new code from the forums or when I need to help my friends with specific C# problem. The problem is I don't want to create new "project" in Visual Studio each time just to code/run only this small code.

有时我想测试来自论坛的新代码,或者我需要帮助我的朋友解决特定的 C# 问题。问题是我不想每次都在 Visual Studio 中创建新的“项目”只是为了编码/运行这个小代码。

Java & Eclipse IDE comes up with "Scrapbook" feature which allows the execution of arbitrary Java expressions. What are the choices for C# programmers?

Java 和 Eclipse IDE 提供了“剪贴簿”功能,允许执行任意 Java 表达式。C#程序员有哪些选择?

采纳答案by P Daddy

Snippet Compileris great for this.

Snippet Compiler非常适合这一点。

LINQPadis also good for testing out all sorts of C#, F# or VB code, not just LINQ queries.

LINQPad也适用于测试各种 C#、F# 或 VB 代码,而不仅仅是 LINQ 查询。



EDIT

编辑

I suppose it's time that I mention that Snippet Compiler hasn't been updated in over five years, and is therefore no longer the best option.

我想是时候提到 Snippet Compiler 已经超过五年没有更新了,因此不再是最佳选择。

However, I undersold LINQPad originally. As Will Dean mentioned in the comments, LINQPad is excellent for all sorts of code, not just LINQ queries. In fact, it has become an indispensable tool for me. I use it daily, not only to quickly test out short snippets, but for relatively complex mini programs. Its advanced output formatting makes it extremely easy to quickly examine intermediate and final results.

但是,我最初低估了 LINQPad。正如 Will Dean 在评论中提到的,LINQPad 非常适合各种代码,而不仅仅是 LINQ 查询。事实上,它已经成为我不可或缺的工具。我每天都使用它,不仅可以快速测试短片,还可以测试相对复杂的小程序。其先进的输出格式使得快速检查中间和最终结果变得非常容易。

回答by Greg Dean

I usually use csc directly from the command line

我通常直接从命令行使用 csc

C:\Users\Greg>csc /o- /debug+ Test.cs

C:\Users\Greg>csc /o- /debug+ Test.cs

回答by JoshBerke

This is something I've never liked about visual studio. One thing (and probally the only thing) I miss from VB6 was the ability to create a project, and run it without saving it or anything. Was great for just what you said.

这是我从不喜欢 Visual Studio 的地方。我从 VB6 中想念的一件事(可能是唯一的一件事)是能够创建一个项目,并在不保存它或任何东西的情况下运行它。非常适合你所说的。

I typically have at least one or two scrap projects of each type, (Console, Winforms, and Web), that I just rotate and use. This way I'm not always creating new projects.

我通常至少有一个或两个每种类型的废弃项目(控制台、Winforms 和 Web),我只是轮换和使用。这样我并不总是在创建新项目。

回答by Jay Bazuzi

Here's what I do:

这是我所做的:

  • Go to Tools->Options->ProjectsAndSolutions, and disable "Save new projects when created".

  • Fire up a new VS, Ctrl-Shift-N to create a project, and use that.

  • Use C# Express to have something that launches more quickly and has less clutter, but still enough power to do most things.

  • 转到工具->选项->项目和解决方案,并禁用“创建时保存新项目”。

  • 启动一个新的 VS,Ctrl-Shift-N 来创建一个项目,然后使用它。

  • 使用 C# Express 可以让一些东西启动得更快、混乱更少,但仍然有足够的能力来做大多数事情。

回答by CMS

回答by dbones

you could have a look into C# Scripting.

你可以看看 C# Scripting。

C# scripting

C# 脚本

this is ment to allow you to build fast scripts without the use of the IDE.

这是为了允许您在不使用 IDE 的情况下构建快速脚本。

HTH

HTH

bones

骨头

回答by Marc Gravell

I always keep a copy of Express Edition running; this can execute and debug projects without needing a proper file location (as long as you don't hit save). Does the job for me...

我总是保留一份 Express Edition 运行;这可以在不需要正确文件位置的情况下执行和调试项目(只要您不点击保存)。对我来说工作吗...

回答by Robert Rossney

I just use the scratch console application project that I've defined for just this purpose. I cut out whatever is in the main program and go to town. If I decide that I need to keep whatever I've prototyped, I create a new project and copy to it. Since I do this all the time, the project's always in my recent-projects list, too.

我只是使用我为此目的而定义的临时控制台应用程序项目。我删除了主程序中的任何内容,然后前往镇上。如果我决定需要保留我的原型,我会创建一个新项目并将其复制到其中。由于我一直这样做,因此该项目也总是在我最近的项目列表中。

回答by user48228

I use a batch file (csc.bat):
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc %1 %2 %3 %4 %5 %6 %7 %8 %9

我使用批处理文件 (csc.bat):
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc %1 %2 %3 %4 %5 %6 %7 %8 %9

For vb.net I use this batch file(vbc.bat):
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc %1 %2 %3 %4 %5 %6 %7 /r:system.windows.forms.dll /r:system.data.dll /r:system.dll

对于 vb.net,我使用这个批处理文件(vbc.bat):
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc %1 %2 %3 %4 %5 %6 %7 /r:system. windows.forms.dll /r:system.data.dll /r:system.dll

For example: C:>csc testit.cs

例如:C:>csc testit.cs

Each will create a .net .exe file. This solution is not so good for Windows Forms applications, however.

每个都将创建一个 .net .exe 文件。但是,此解决方案对于 Windows 窗体应用程序并不是那么好。

回答by Cory Foy

There's also the interactive C# shell, built on top of Mono:

还有基于 Mono 构建的交互式 C# shell:

http://mono-project.com/CsharpRepl

http://mono-project.com/CsharpRepl

That's similar to what other languages have (for example, irb with Ruby)

这类似于其他语言所拥有的(例如,带有 Ruby 的 irb)