CSharpRepl is a cross-platform command-line Read-Eval-Print-Loop for C# with syntax highlighting and NuGet support, developed by Will Fuqua. It supports IntelliSense and referencing of local .NET projects or assemblies.
C# REPL is a .NET 6 global tool, which can be run on Windows, Mac OS, or Linux. The tool can be installed using the dotnet CLI:
dotnet tool install -g csharprepl
According the documentation of C# REPL, if you're running on Mac OS Catalina (10.15) or later, make sure you follow any additional directions printed to the screen. You may need to update your PATH variable in order to use .NET global tools.
After installation, you can invoke the tool by using the command csharprepl
.
Simply type some C# codes into the prompt and press Enter
to run them. The result, if any, will be printed:
> Console.WriteLine("It works !");
It works !
> var x = 12 * 4;
> x
48
If a statement is not a "complete statement", a newline will automatically be inserted when Enter
is pressed.
> if (x == 5)
| // caret position, after we press Enter on Line 1
To evaluate multiple lines of code, use Shift+Enter
to insert a newline:
> var time = "11:12:24";
var timeToArray = time.Split(":");
Console.WriteLine(timeToArray[0]);
11
We can also write functions and call them like bellow:
> public static int RandomNumber(int min, int max)
{
System.Random random = new System.Random();
return random.Next(min, max);
}
> var randomNumber = RandomNumber(50, 60);
> Console.WriteLine(randomNumber)
51
Use the #r
command to add assembly or nuget references.
#r "AssemblyName"
or #r "path/to/assembly.dll"
#r "path/to/project.csproj"
. Solution files (.sln) can also be referenced.#r "nuget: PackageName"
to install the latest version of a package, or #r "nuget: PackageName, 13.0.5"
to install a specific version (13.0.5 in this case).> #r "nuget: RESTCountries.NET"
Adding references for RESTCountries.NET.2.0.0
> using static RESTCountries.Services.RESTCountriesAPI;
> var countries = await GetAllCountriesAsync();
> Console.WriteLine(countries.Count)
250
>
.NET Apps can also be run inside the REPL, start the csharprepl
tool with the --framework
parameter, by specifying the Microsoft.AspNetCore.App
shared framework. Then, use the #r
command to reference the application DLL. See the Command Line Configuration for more details.
csharprepl --framework Microsoft.AspNetCore.App
Ctrl+C
- Cancel current lineCtrl+L
- Clear screenEnter
- Evaluate the current line if it's a syntactically complete statement; otherwise add a newlineControl+Enter
- Evaluate the current line, and return a more detailed representation of the resultShift+Enter
- Insert a new line (this does not currently work on Linux or Mac OS; Hopefully this will work in .NET 7)Ctrl+Shift+C
- Copy current line to clipboardCtrl+V
, Shift+Insert, and Ctrl+Shift+V - Paste text to prompt. Automatically trims leading indentF1
- Opens the MSDN documentation for the class/method under the caret (example)F9
- Shows the IL (intermediate language) for the current statement in Debug mode.Ctrl+F9
- Shows the IL for the current statement with Release mode optimizations.F12
- Opens the source code in the browser for the class/method under the caret, if the assembly supports Source Link.Ctrl+Space
- Open autocomplete menu. If there's a single option, pressing Ctrl+Space again will select the optionEnter
, Right Arrow, Tab - Select active autocompletion optionEscape
- closes autocomplete menuHome and End
- Navigate to beginning of a single line and end of a single line, respectivelyCtrl+Home
and Ctrl+End
- Navigate to beginning of line and end across multiple lines in a multiline prompt, respectivelyArrows
- Navigate characters within textCtrl+Arrows
- Navigate words within textCtrl+Backspace
- Delete previous wordCtrl+Delete
- Delete next wordAs C# REPL is a standalone tool, you can invoke it from your VS Code Terminal, Rider Terminal or from any other IDE that have an embedded terminal emilator.
dotnet tool uninstall -g csharprepl
Quick Links