How to quickly encode and run small C # code

There are times when I want to test new code from forums or when I need to help my friends with a specific C # problem. The problem is that I do not want to create a new “project” in Visual Studio every time, just to code / run only this small code.

In the Java and Eclipse IDEs, the Scrapbook function appears, which allows arbitrary Java expressions to be executed. What are the options for C # programmers?

+54
c # visual-studio testing
Dec 21 '08 at 18:12
source share
18 answers

Snippet Compiler is great for this.

LINQPad is also good for testing LINQ code.




EDIT

I guess this is the time when I mention that Snippet Compiler has not been updated for more than five years and therefore is no longer the best option.

However, I first abandoned LINQPad. As Dean noted in the comments, LINQPad is great for all kinds of code, not just LINQ queries. In fact, it has become an indispensable tool for me. I use it daily, not only for quick testing of short fragments, but for relatively complex mini-programs. Its advanced output formatting allows you to very quickly learn intermediate and final results.

+49
Dec 21 '08 at 18:15
source share

That's what I'm doing:

  • Go to Tools-> Options-> ProjectsAndSolutions and turn off "Save new projects when creating."

  • Run the new VS, Ctrl-Shift-N to create the project, and use it.

  • Use C # Express to have something that runs faster and has less clutter, but still has enough power for most things.

+18
Dec 21 '08 at 18:17
source share

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

From your site:

We are a group of .NET developers who are tired of starting Visual Studio, creating a new project and launching it, just to test simple code or try samples from other developers.

This tool was inspired by http://jsfiddle.net , which is simply awesome.

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

+17
Nov 12 '14 at 1:27
source share

A bit late for the party, but I came here looking for it myself.

The best suitable solution for me was to use C# Interactive -window. You can access it by opening it using View > Other Windows > C# Interactive or by selecting the C # code and clicking 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 that it works in VS2015, I do not think that it works in older versions.

+9
Aug 01 '16 at 12:24
source share

I usually use csc directly from the command line

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

+7
Dec 21 '08 at 18:15
source share
+3
Dec 21 '08 at 18:19
source share

There is also an interactive C # shell built on top of Mono:

http://mono-project.com/CsharpRepl

What looks like other languages ​​(like irb with Ruby)

+3
Dec 22 '08 at 2:11
source share

You can run many things in the Immediate window (Debug -> Windows -> Immediate)

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

+3
Jan 14 '13 at 14:30
source share

I just use the console console application project, which I defined just for this purpose. I cut out everything in the main program and go to the city. If I decide that I need to save everything that I created, I create a new project and copy it. Since I do this all the time, the project is always on my list of recent projects.

+2
Dec 21 '08 at 21:04
source share

I am using 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. However, this solution is not well suited for Windows Forms applications.

+2
Dec 22 '08 at 1:30
source share

See Introducing Microsoft's Roslyn CTP .

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

+2
May 24 '12 at 20:34
source share

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

+2
Feb 02 '15 at 21:36
source share

This is something I never liked in a visual studio. One thing (and probably the only thing) that I missed from VB6 is the ability to create a project and run it without saving it or anything else. It was perfect for what you said.

Usually I have at least one or two scrap projects of each type (Console, Winforms and Web) that I just rotate and use. Thus, I do not always create new projects.

+1
Dec 21 '08 at 18:16
source share

you could take a look at C # Scripting.

C # scripting

This means that you can create quick scripts without using the IDE.

NTN

bones

+1
Dec 21 '08 at 18:20
source share

I always keep a copy of the Express version; this can execute and debug projects without requiring the correct location of the files (until you delete the save). Does it work for me ...

+1
Dec 21 '08 at 20:56
source share

Try SharpDevelop. It does not contain everything, but is good for quick examples. You can open the solution and easily replace the code to check something else. In the project properties, you can change the solution from the console to the application window by selecting combobox. You can easily translate code from C # to Boo, Python, Ruby or VB, or from any of these languages ​​to another, starting from your source. Therefore, if you are looking for a solution to the problem on the Internet and cannot find it in VB, but can find it in C #, you can convert it.

+1
Mar 31 2018-11-11T00:
source share

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

+1
Sep 13 '13 at 11:01
source share

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

+1
Oct 09 '14 at 17:49
source share



All Articles