How to quickly code and run small C# code

Viewed 40478

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.

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

20 Answers

Snippet Compiler is great for this.

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


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.

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.

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.

I usually use csc directly from the command line

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

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

http://mono-project.com/CsharpRepl

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

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.

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

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

For example: C:>csc testit.cs

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

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.

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.

you could have a look into C# Scripting.

C# scripting

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

HTH

bones

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...

Related