HomeAuthorContact
Open Source
CsharpRepl, the magic command line C# REPL
Laurent Egbakou
Laurent Egbakou
November 12, 2021
2 min
Our work may have helped you ?
Support Us

Table Of Contents

01
What is csharpRepl ?
02
Installation
03
Usage
04
Integrating with other softwares
05
Uninstallation
06
Submit an issue or PR

What is csharpRepl ?

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.

Installation

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.

Usage

After installation, you can invoke the tool by using the command csharprepl.

Intellisense

Code evaluation

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

Adding references

Use the #r command to add assembly or nuget references.

  • For assembly references, run #r "AssemblyName" or #r "path/to/assembly.dll"
  • For project references, run #r "path/to/project.csproj". Solution files (.sln) can also be referenced.
  • For nuget references, run #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

Keyboard Shortcuts

  • Basic Usage
    • Ctrl+C - Cancel current line
    • Ctrl+L - Clear screen
    • Enter - Evaluate the current line if it's a syntactically complete statement; otherwise add a newline
    • Control+Enter - Evaluate the current line, and return a more detailed representation of the result
    • Shift+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 clipboard
    • Ctrl+V, Shift+Insert, and Ctrl+Shift+V - Paste text to prompt. Automatically trims leading indent
  • Code Actions
    • F1 - 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.
  • Autocompletion
    • Ctrl+Space - Open autocomplete menu. If there's a single option, pressing Ctrl+Space again will select the option
    • Enter, Right Arrow, Tab - Select active autocompletion option
    • Escape - closes autocomplete menu
  • Text Navigation
    • Home and End - Navigate to beginning of a single line and end of a single line, respectively
    • Ctrl+Home and Ctrl+End - Navigate to beginning of line and end across multiple lines in a multiline prompt, respectively
    • Arrows - Navigate characters within text
    • Ctrl+Arrows - Navigate words within text
    • Ctrl+Backspace - Delete previous word
    • Ctrl+Delete - Delete next word

Integrating with other softwares

As 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.

Uninstallation

dotnet tool uninstall -g csharprepl

Submit an issue or PR


Tags

#oss#csharprepl#csharp

Share


Laurent Egbakou

Laurent Egbakou

Microsoft MVP | Founder

Microsoft MVP | Software Engineer ☁️ | Blogger | Open source contributor

Expertise

.NET
Go
Python
Cloud
Open Source

Social Media

linkedintwitterwebsitegithub
Microsoft MVPAzure Developer Associate Badge

Related Posts

RESTCountries.NET
Getting started with RESTCountries.NET
December 19, 2021
2 min
File size from Base64 string
dotNET
Calculate a file size from Base64 string
February 20, 2022
2 min
© 2024, All Rights Reserved.
Powered By @lioncoding

French Content 🔜

Quick Links

Advertise with usAbout UsContact UsHire Us

Social Media