DEV Community

Cover image for C# REPL
Talles L
Talles L

Posted on • Edited on

C# REPL

Having a quick C# REPL can help a lot on testing little snippets of code.

.NET Fiddle

The quickest one is probably .NET Fiddle, which can be accessed by your web browser and don’t required any setup or registering:

csi.exe

There’s a C# REPL in Visual Studio on View > Other Windows > C# Interactive:

Which opens:

You can also fire csi.exe directly outside of Visual Studio:

Having Visual Studio 2017 Enterprise and Visual Studio 2019 Community installed on my Windows machine, I’ve managed to find it on:



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

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csi.exe


Enter fullscreen mode Exit fullscreen mode

dotnet-script

It seems that csi.exe is not available for folks using .NET Core on Linux yet. Fortunately, there’s dotnet-script:



$ dotnet tool install -g dotnet-script
You can invoke the tool using the following command: dotnet-script
Tool 'dotnet-script' (version '0.53.0') was successfully installed.
$ dotnet script
> Math.Pow(3, 2)
9


Enter fullscreen mode Exit fullscreen mode

csharprepl (.NET 7)

If you are already on .NET 7 you can install csharprepl:



dotnet tool install -g csharprepl


Enter fullscreen mode Exit fullscreen mode

Just call csharprepl from your terminal and you'll drop into a fully functional REPL (syntax highlight, auto complete, documentation, etc):

Other alternatives

I haven’t tested it myself, but a quick search gives LINQPad and CS-Script as alternatives.

Top comments (3)

Collapse
 
uchitesting profile image
UchiTesting

Very nice. I knew about CSI but I hardly ever used it. They should provide it with the .NET SDK and showcase it. This is a cool feature. Java has jShell which has a bit more visibility IMHO.
I also use .NET Fiddle and that is very nice service. Now the main drawback I meet is even-though you set auto run to false, it runs anyway. It happened several times with infinite loops I could not fix because of that. Also there are limitations with some features (or I did not manage to make them work, idk).

So then I simply made a fiddle project so that I use VS Code in such case. I then have no limitation on any package to import.

Collapse
 
mosthated profile image
MostHated

I was interested in comparing the performance on Linux between PowerShell and dotnet-script, so I ran a script using both PSCore 7.x script using pwsh test1.ps1, the other was a C# csx using dotnet-script test1.csx.

I was expecting Powershell to take the win since the C# script had to compile, but the C# script blew it away in overall time taken from hitting enter until the terminal came back complete. It was just taking in a parameter, doing a simple loop over it a few times and printing the results.

The .ps1 script took between 5s30ms to 7s50ms
The .csx took between 0.23ms and 0.26ms.
I was quite impressed. I don't know if the csx has to run the dotnet runtime in the same way or in the same capacity as running a ps1 script? I expected the compilation to have to occur, then run the dotnet runtime, etc.

Either way, with those results, I might have to reconsider using PS for my scripts on linux and just go with C# instead.

Collapse
 
tallescs profile image
tallescs

You are the first other Talles developer that I found. Also brazilian and c#.
Instant follow