15

Is there an interactive shell for programming quick code snippets in C#/.NET? Sometimes I'm interested in trying out a quick block of .NET code without creating a whole console application (that I then have to delete when I'm done).

I understand that the Immediate Window sort of provides this functionality, but it seems like you have to have an application open and be debugging it for the Immediate Window to be of any use.

I was tinkering with Ruby a while back, and the Interactive Ruby Shell (irb) was an invaluable little tool for trying out little snippets of Ruby code. Lightweight and instantaneous. This kind of thing seems like a no-brainer to me. Does anything like it exist for C#/.NET?

Michael
  • 1,968
  • 4
  • 24
  • 40
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1187423/anders-hejlsbergs-c-4-0-repl – rtalbot Aug 29 '11 at 17:10
  • 1
    possible duplicate of [C# REPL tools; quick console-like compiling tool](http://stackoverflow.com/questions/2204823/c-repl-tools-quick-console-like-compiling-tool) – Ahmad Mageed Aug 29 '11 at 17:10
  • Not interactive, but SnippetCompiler can compile snippets, throw in some Console.Writelines, and it's still very useful: http://www.sliver.com/dotnet/SnippetCompiler/. – Tim Lloyd Aug 29 '11 at 17:11
  • Thanks for the duplicate links. Man, I tried searching for everything to find this; I was sure someone had already asked it, but unless you know what you're looking for (e.g., "REPL"), you're kind of out of luck. "C#" and "console" are useless search terms for finding anything on this subject. – Michael Aug 29 '11 at 17:20

10 Answers10

21

LINQPad is very neat for that:

LINQPad is more than a LINQ tool: it's an ergonomic C#/VB/F# scratchpad that instantly executes any expression, statement block or program with rich output formatting – the ultimate in dynamic development. Put an end to those hundreds of Visual Studio Console projects cluttering your source folder!

driis
  • 161,458
  • 45
  • 265
  • 341
6

There isn't a full interactive shell for C#. However, for many things, you can use LINQPad to quickly try out a snippet of C# code. While it's intended for LINQ testing, it works for other C# code as well.

For details, see LINQPad as a Code Snippet IDE.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

The Mono project has an interactive shell.

Also, you can use the Snippet Compiler.

Jordão
  • 55,340
  • 13
  • 112
  • 144
2

Yes, there's one for mono mono-project.com/CsharpRepl

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • dotnet tool install csharprepl --global – egrubbs Mar 22 '23 at 20:50
  • The only downside with csharprepl is that it seems to require a full file path. However, it's not that bad since #load "C:\\..." will let you hit tab to autocomplete the file path. – egrubbs Mar 22 '23 at 20:52
2

I know this is an old post, but I am new to C#. I actually found a dotnet tool dotnet-repl that is pretty much like Java's jshell. To install simply go to powershell and enter:

dotnet tool install dotnet-repl --global

dotnet-repl.exe

Screen Shot of dotnet-repl

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
1

We've just released a small REPL tool for C# with code completion and everything called CShell. http://cshell.net

You can also write scrips and then send them to the REPL or just write your snippets right in the REPL.

It's still in beta, but it works pretty well!

Screenshot of CShell

lukebuehler
  • 4,061
  • 2
  • 24
  • 28
1

There was one published by the Mono Project a while back.

Ta01
  • 31,040
  • 13
  • 70
  • 99
0

.Net Fiddle https://dotnetfiddle.net/ is an option. Not exactly an interactive environment where you can try out any expression. In .Net Fiddle, you can change the project type, compiler version, etc.

Mitesh Budhabhatti
  • 1,583
  • 2
  • 16
  • 20
0

LINQPad is very good, but if you are into a quick (no installation) C# snippet testing without external dependencies (files, databases etc.) you can also use Rextester.

However, notice that its compiler version is 4.0.30319.17929, so fancy things from C# 6.0 like interpolated strings, null conditional, property initializers will not be available.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
-1

It is called snippetcompiler, will search for the url...

Mario S
  • 11,715
  • 24
  • 39
  • 47
Davide Piras
  • 43,984
  • 10
  • 98
  • 147