How to quickly code and run small C# code

C#Visual StudioTesting

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

A bit late to the party, but I came here while searching for this myself.

The best suitable solution for me was using the C# Interactive-window inside Visual Studio 2015 or later. You can access it by opening it via View > Other Windows > C# Interactive, or by selecting some c# code and clicking on Execute in Interactive in the right-click context menu.

Here is a link on how to use it: http://dailydotnettips.com/2016/01/12/use-c-interactive-window-for-your-coding-experiment-in-visual-studio-2015/

I know it works in VS2015, I don't think it works in older versions.

Solution 3 - C#

The website .NET Fiddle seems like a nice option. It has code completion, code timing, and NuGet Package integration.

From their site:

> We are a group of .NET developers who are sick and tired of starting > Visual Studio, creating a new project and running it, just to test > simple code or try out samples from other developers.
> > This tool was inspired by http://jsfiddle.net, which is just awesome.

In particular, I recommend trying out their MVC mode. On the left side of the screen choose Project Type: MVC.

Solution 4 - C#

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.

Solution 5 - C#

I usually use csc directly from the command line

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

Solution 6 - C#

You can run a lot of things in the Immediate window (Debug >> Windows >> Immediate)

For example, you can run the following: System.IO.File.WriteAllText(@"c:\temp\blah.txt", "Hi there!");

Solution 7 - C#

Solution 8 - C#

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

http://mono-project.com/CsharpRepl">http://mono-project.com/CsharpRepl</a>

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

Solution 9 - C#

Have a look at Introducing the Microsoft “Roslyn” CTP.

The "C# Interactive" window is perfect for this.

Solution 10 - C#

Late in here, but if you also want to share the code with others, then .NET Fiddle is the best I have tried so far. For offline solutions, LINQPad definitely would have my vote as well, and not just for LINQ queries.

Solution 11 - C#

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.

Solution 12 - C#

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.

Solution 13 - C#

RoslynPad - nice lightweight open-source alternative to LINQPad.

RoslynPad, https://roslynpad.net/

GitHub: https://github.com/aelij/RoslynPad

Solution 14 - C#

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.

Solution 15 - C#

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

Solution 16 - C#

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

Solution 17 - C#

Try SharpDevelop. It doesn't contain everything, but is good for quick examples. You can open a solution and easily swap out the code to test something else. In the project properties you can change a solution from console to window app via a combobox selection. You can easily translate your code from C# to Boo, Python, Ruby or VB, or from any of these languages to another, starting with whatever your source is. Therefore, if you go looking for a problem solution on the web and can't find one in VB - but do find one in C#, you may be able to convert it.

Solution 18 - C#

There is an online web C# compiler and executor: http://www.compileonline.com/compile_csharp_online.php

Solution 19 - C#

You can try using ScriptCS. It uses the Roslyn compiler to create a scripting language using C#.

Solution 20 - C#

Visual Studio should be a better choice, but it is too heavy. Some simple tests, I often try here. > https://docs.microsoft.com/zh-cn/learn/modules/csharp-write-first/2-exercise-hello-world

or use linqpad and vscode > https://www.linqpad.net/
> https://code.visualstudio.com/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGantView Question on Stackoverflow
Solution 1 - C#P DaddyView Answer on Stackoverflow
Solution 2 - C#Richard BosView Answer on Stackoverflow
Solution 3 - C#twasbrilligView Answer on Stackoverflow
Solution 4 - C#Jay BazuziView Answer on Stackoverflow
Solution 5 - C#Greg DeanView Answer on Stackoverflow
Solution 6 - C#Byron KatzView Answer on Stackoverflow
Solution 7 - C#Christian C. SalvadóView Answer on Stackoverflow
Solution 8 - C#Cory FoyView Answer on Stackoverflow
Solution 9 - C#rbraybView Answer on Stackoverflow
Solution 10 - C#Johny SkovdalView Answer on Stackoverflow
Solution 11 - C#Robert RossneyView Answer on Stackoverflow
Solution 12 - C#user48228View Answer on Stackoverflow
Solution 13 - C#Johan HelsénView Answer on Stackoverflow
Solution 14 - C#JoshBerkeView Answer on Stackoverflow
Solution 15 - C#dbonesView Answer on Stackoverflow
Solution 16 - C#Marc GravellView Answer on Stackoverflow
Solution 17 - C#Joe AView Answer on Stackoverflow
Solution 18 - C#David Silva SmithView Answer on Stackoverflow
Solution 19 - C#BrianView Answer on Stackoverflow
Solution 20 - C#Brucelin MichaelView Answer on Stackoverflow