DEV Community

loading...
Cover image for C# REPL

C# REPL

Talles L
A Brazilian software developer 🇧🇷 that loves to play video-games 🎮, tries it best to be active on GitHub 💻, and sometimes writes here on DEV 📄
Updated on ・1 min read

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

Other alternatives

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

Discussion (1)

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.