C# REPL outside Visual Studio
Asked Answered
I

9

40

F# has a REPL (read–eval–print loop) F# Interactive, C:\Program Files (x86)\Microsoft F#\v4.0\Fsi.exe.

I understand C# now has its own interactive REPL, as released with Roslyn. How do I open outside Visual Studio? Where is csi.exe?

Iseabal answered 11/6, 2012 at 13:0 Comment(4)
I thought that was one of the samples ... at the very least, Anders created a Console based REPL in the very first Roslyn presentation available online. If you can find the presentation, the code is very simple, you can write it easily based on that.Pretended
There is one now :) as you can see by my answer belowHylomorphism
That MSDN link now redirects to the wikipedia entry for REPL :|Demolish
The best Option is using CSharpRepl take a look at my answer: https://mcmap.net/q/13746/-c-repl-outside-visual-studioBottom
M
22

C# Interactive window and csi.exe REPL were added to Visual Studio 2015 Update 1 (emphasis mine):

Introducing Interactive

The Interactive Window is back! The C# Interactive Window returns in Visual Studio 2015 Update 1 along with a couple other interactive treats:

  • C# Interactive. The C# Interactive window is essentially a read-eval-print-loop (REPL) that allows you to play and explore with .NET technologies while taking advantage of editor features like IntelliSense, syntax-coloring, etc. Learn more about how to use C# Interactive on Channel 9 or by reading our beginner’s walkthrough.

  • csi. If you don’t want to open Visual Studio to play around with C# or run a script file, you can access the interactive engine from the Developer Command Prompt. Type csi /path/myScript.csx to execute a script file or type simply csi to drop inside the command-line REPL.

  • Scripting APIs. The Scripting APIs give you the ability to execute snippets of C# code in a host-created execution environment. You can learn more about how to create your own C# script engine by checking out our code samples.

See What’s New in Visual Studio 2015 Update 1 for .NET Managed Languages.


https://www.visualstudio.com/en-us/news/vs2015-update1-vs.aspx

>csi
Microsoft (R) Visual C# Interactive Compiler version 1.1.0.51109
Copyright (C) Microsoft Corporation. All rights reserved.

Type "#help" for more information.
> #help
Keyboard shortcuts:
  Enter         If the current submission appears to be complete, evaluate it.  Otherwise, insert a new line.
  Escape        Clear the current submission.
  UpArrow       Replace the current submission with a previous submission.
  DownArrow     Replace the current submission with a subsequent submission (after having previously navigated backwards).
REPL commands:
  #help         Display help on available commands and key bindings.
Script directives:
  #r            Add a metadata reference to specified assembly and all its dependencies, e.g. #r "myLib.dll".
  #load         Load specified script file and execute it, e.g. #load "myScript.csx".
>
> Enumerable.Range(10)
(1,12): error CS7036: There is no argument given that corresponds to the required formal parameter 'count' of 'Enumerable.Range(int, int)'
> Enumerable.Range(1, 10)
RangeIterator { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
Michalmichalak answered 1/12, 2015 at 23:33 Comment(1)
Brilliant, let me try it outIseabal
H
18

I created exactly what you are asking for, a Roslyn based REPL running outside VisualStudio.

You can download it from PoC - Roslyn C# ScriptEngine Execute v1.0.exe which is a stand-alone exe containing all Roslyn assemblies as embedded resources. See also Video: Using O2's Roslyn-based C# REPL Script environment

Note that I have developed a more powerful C# REPL for the O2 Platform, which you can see and download from C# REPL Script Environment

On the topic of Roslyn, here are a number of other stand-alone tools that I created:

Note that all these tools and the O2 Platform are released under an Open Source license, so please fell free to expand and make them better :)

Hylomorphism answered 3/8, 2012 at 3:49 Comment(7)
Files not available anymore.Chigoe
Yeah, Dropbox is being ridiculous (they are blocking all downloads, and worse of all are not telling me what which files are the problem :( )Hylomorphism
OK, I finally found a place to put these files again. They are now at bintray. Please take a look at bintray.com/o2-platform/O2-Tools/Roslyn and let me know if it works okHylomorphism
@DinisCruz Roslyn C# was not found.. can u please give another link?Esperanto
Hi @Esperanto I just noticed that the # in the C# description was breaking the url, can you try using dl.bintray.com/o2-platform/O2-Tools/… , the other links at bintray.com/o2-platform/O2-Tools/Roslyn seem to be okHylomorphism
Btw, to ask questions about these tools please use the O2 Platform mailing list blog.diniscruz.com/p/o2-mailing-list.html Here is a good description of the main APIs github.com/o2platform/FluentSharp and this post as tons of links blog.diniscruz.com/2014/09/…Hylomorphism
Hey, can you update your links in the actual answer instead of the comments? It would make their use much more likely.Thunderstone
A
11

If you're using dotnet core then try dotnet-script.

run dotnet tool install --global dotnet-script to install. Then run dotnet-script to get a REPL style command prompt

> Console.WriteLine("Hello world!");
Hello world!

Info found here

Annecorinne answered 4/3, 2020 at 14:36 Comment(2)
@GabrielSilva take a look at my answer to this question you may like it.Bottom
more helpful to me than the answer above. thanks.Carlist
B
6

The only executable I found is C:\Program Files (x86)\Microsoft Codename Roslyn CTP\Binaries\rcsi.exe, which can execute .csx files. But it doesn't seem to actually support REPL.

Blowout answered 11/6, 2012 at 13:32 Comment(1)
Same for me (new CTP) only C:\Program Files (x86)\Microsoft Roslyn CTP\Binaries\rcsi.exeIseabal
S
6

How about scriptcs: http://scriptcs.net/?

There is a REPL and you even get the power of NuGet.

Snooze answered 19/11, 2014 at 7:57 Comment(0)
I
4

It may not be a substitute but I mainly use RoslynPad for quick research and test. I've also used Linqpad. RoslynPad is "A simple C# editor based on Roslyn" according to their website https://roslynpad.net/. You can write small scripts and you can see variables values' and you may also save them.

Indecorum answered 23/12, 2017 at 14:49 Comment(0)
P
3

Anders Hejlsberg builds a C# REPL loop in this video, 65 minutes in:

http://channel9.msdn.com/Events/PDC/PDC08/TL16#time=65m00s

It's a much older version of Roslyn, but the same ideas apply (and eval! ;).

Pretended answered 21/7, 2012 at 11:36 Comment(0)
B
3

use powershell find csi.exe:

PS > Get-ChildItem "C:\Program Files (x86)\" -Recurse | Where-Object Name -CMatch "csi.exe"

below is my output:



    Directory: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----        12/9/2018   6:42 PM          15712 csi.exe                                                                                                                     
-a----        12/9/2018   6:42 PM           8482 csi.exe.config                                                                                                              


    Directory: C:\Program Files (x86)\Microsoft Visual Studio\Shared\Packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\tools\Roslyn45


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----        5/24/2018   1:38 PM          13680 csi.exe                                                                                                                     


    Directory: C:\Program Files (x86)\Microsoft Visual Studio\Shared\Packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\tools\RoslynLatest


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----        5/24/2018   1:38 PM          15280 csi.exe                                                                                                                     
-a----        5/24/2018   1:38 PM           8816 csi.exe.config                                                                                                              


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


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----        6/27/2016   2:50 PM          13680 csi.exe                                                                                                                     


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


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----         3/7/2017  10:56 AM          14192 csi.exe                                                                                                                     
-a----         3/7/2017  10:56 AM           7905 csi.exe.config                                                                                                              


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


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----        4/13/2017   3:47 PM          14192 csi.exe                                                                                                                     
-a----        4/13/2017   3:47 PM           8006 csi.exe.config                                                                                                              


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


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----        9/21/2017  11:42 PM          15312 csi.exe                                                                                                                     
-a----        9/21/2017  11:42 PM           8816 csi.exe.config                                                                                                              


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


Mode                LastWriteTime         Length Name                                                                                                                        
----                -------------         ------ ----                                                                                                                        
-a----       12/13/2017  10:09 PM          16296 csi.exe                                                                                                                     
-a----       12/13/2017  10:09 PM           8816 csi.exe.config                                                                                                              

Birdcage answered 1/7, 2019 at 6:53 Comment(0)
B
1

Best Option in 2023

The best option nowadays is using CSharpRepl. Action talks louder than words:
Gif for Sample usage

Install it using this command dotnet tool install -g csharprepl. And to use it run this command in terminal csharprepl
It has many features you can even install nuget packages or use your own code by referencing to the assembly of it. Documentation

Bottom answered 25/7, 2023 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.