Is there an interactive interpreter for C#? [closed]
Asked Answered
H

12

78

Sometimes it's handy to have access to your language to do quick things without starting Visual Studio and creating a new console app.

Is there something like Python's interactive mode or groovy shell, except for C#?

Howard answered 13/1, 2010 at 17:13 Comment(2)
Duplicate/Similar Question - https://mcmap.net/q/13498/-c-console-closedEntomophilous
Visual Studio 2015 Update 1 includes a C# Interactive window.Adeno
B
114

Update for 2022

After installing Visual Studio 2022, add the following to your PATH environment variable.

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Packages\Microsoft.Net.Compilers.2.6.1\tools

Then open your terminal (CMD, PowerShell, Windows Terminal) and type csi to run C Sharp Interactive.

You'll get something like this:

PS C:\> csi
Microsoft (R) Visual C# Interactive Compiler version 2.6.1.62414
Copyright (C) Microsoft Corporation. All rights reserved.

Type "#help" for more information.
> var list = new List<int>{ 1, 2, 3, 4 };
> list // You don't need to call Console.WriteLine() to see values
List<int>(4) { 1, 2, 3, 4 }
> // You can keep adding lines as needed

Previous Answer

With the Visual Studio 2015 Update 1 there now is a C# Interactive tool window built into Visual Studio.

The new tool window is invoked by going to ViewOther WindowsC# Interactive.

For Visual Studio 2010 to 2013 you can use the Roslyn CTP to get a similar tool window in Visual Studio.

Bogosian answered 20/12, 2011 at 20:0 Comment(5)
Install from msdn.microsoft.com/en-US/roslyn then 'view / other windows / c# interactive'Strake
This is the most correct answer now.Unmoving
And it is available in Visual Studio 2015 Update 1. Link: visualstudio.com/en-us/news/vs2015-update1-vs.aspx#CsharpAdeno
The command line version can be found at C:\Program Files (x86)\MSBuild\14.0\Bin\csi.exe (automatically in the PATH in the VS developer command prompt)Leftwich
Correct path for me (v17.10.2) was C:\Program Files (x86)\Microsoft Visual Studio\Shared\Packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\tools\RoslynLatestAzole
K
37

Have a look at CsharpRepl (part of the Mono project). Never used it myself, I hasten to add.

For LINQ stuff, you should also look at LINQPad.

Krutz answered 13/1, 2010 at 17:15 Comment(7)
Indeed, LINQPad is useful for more than just LINQ!Travertine
LinqPad is a great piece of software but i find Snippet Compiler to be the lightest and quickest for testing snippets. Give it a try as well.Starve
I always run a VS Console App project in the background.Fritzsche
CsharpRepl works quite well. Doesn't interpret files, but there was some development on it.Dilapidation
+1 for LINQPad, I hadn't used that before. I don't have mono installed here, I'll have to check out CsharpRepl later tonight.Howard
CSharpRepl works, but it's a little rough around the edges.Howard
It's worth noting that as nice as LINQPad is, it does not offer a "true" interactive environment like CsharpRepl does (that is, results are not persisted across executions).Chemmy
E
10

There are several.

Entomophilous answered 13/1, 2010 at 17:16 Comment(3)
Well, and the first couple of results are all about the same one ...Kidron
Looking at the first couple of results is usually not a good idea, no matter the search engine. You need to explore the first page or two, perhaps even three, if you are feeling ambitious. This, in addition to trying different search terms really helps broaden your search.Entomophilous
CSI doesn't seem to support the latestfeatures of the language. I found a tip here: codeproject.com/Messages/2635407/… that was supposed to fix this, but I was unable to get that version working properly.Medalist
K
9

Like others noted, Mono's CSharpRepl is probably the right answer. However, if you're not fixed to C#, then PowerShell is a pretty nice environment of playing around with .NET. I frequently use it to test regular expressions, format strings, etc. All the kinds of stuff you have ConsoleProject163 lying around for :)

Kidron answered 13/1, 2010 at 17:18 Comment(0)
T
7

In addition to the other good answers here (+1), there is also CSI and the immediate window in Visual Studio.

Travertine answered 13/1, 2010 at 17:24 Comment(1)
The immediate windows is nice, but you can't define classes on the fly.Medalist
Q
4

Here is a Mono CsharpRepl which is what you are looking for.

This documents the features available in the C# interactive shell that is part of Mono's C# compiler. An interactive shell is usually referred to as a read eval print loop or repl. The C# interactive shell is built on top of the Mono.CSharp (http:/monodoc/N:Mono.CSharp) library, a library that provides a C# compiler service that can be used to evaluate expressions and statements on-the-fly.

Qadi answered 13/1, 2010 at 17:15 Comment(0)
V
4

There is ScriptCS which uses the Roslyn CTP nuget package.

Vitiate answered 9/5, 2013 at 15:52 Comment(1)
ScriptCs now has interactive shell too - codebetter.com/glennblock/2013/05/07/scriptcs-gets-a-replGautama
T
3

I think CShell would be a nice contribution to answers given here. It provides code completion enabled REPL command window as well as a lightweight workspace management tools to include some scratch files and library references.

Traci answered 10/6, 2014 at 23:38 Comment(0)
O
2

With Roslyn CTP there now is a full C# interactive window from Microsoft direct in Visual Studio!! Works great! So far runs all my libraries. After install: Invoking View -> Other Windows -> C# Interactive from the menu.

Offhand answered 27/3, 2014 at 6:55 Comment(0)
W
1

I use ideone.com sometimes, but LINQPad is a good choice if an install is acceptable.

Weekly answered 28/5, 2014 at 9:27 Comment(1)
+1 for reminding me of LINQPad, I have it installed but I rarely use it nowadays. It is not an interactive shell as such but amost. It is so quick to bash down one or more lines and see the output. It is great to get the syntax right with regular expressions for example. (LINQPad can be used for much more than that!)Weyermann
S
0

You can use this developed by Miguel De Icaza, is shipped with mono and have code completion

Sine answered 13/1, 2010 at 17:27 Comment(0)
F
0

If you are looking for a simple web based environment to crank out some C# code try coderpad.io Not sure if you can add libraries n' all but its a quick and simple solution if you are just interested in testing out several commands

Fabrizio answered 13/10, 2013 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.