Skip to content

Latest commit

 

History

History
128 lines (95 loc) · 5.25 KB

README.md

File metadata and controls

128 lines (95 loc) · 5.25 KB

C# REPL

A tool to promote rapid experimentation and exploration of C# expressions, statements, and nuget libraries. Provides a command line REPL tool for C#, supporting intellisense, documentation, and nuget package installation.

C# REPL screenshot

Installation

C# REPL is a .NET 5 global tool. It can be installed via:

dotnet tool install -g csharprepl

And then run by executing csharprepl at the command line.

Usage:

Run csharprepl from the command line to start. The default colors look good on terminals that use dark backgrounds; but these colors can be changed using a theme.json file provided as a command line argument.

Evaluating Code

Type some C# into the prompt and press Enter to run it. Its result, if any, will be printed:

> Console.WriteLine("Hello World")
Hello World

> DateTime.Now.AddDays(8)
[6/7/2021 5:13:00 PM]

If we want to evaluate multiple lines of code, we can use Shift+Enter to insert a newline:

> var x = 5;
  var y = 8;
  x * y
40

Additionally, if the statement is not a "complete statement" a newline will automatically be inserted when Enter is pressed. For example, in the below code, the first line is not a syntactically complete statement, so when we press enter we'll go down to a new line:

> if (x == 5)
  | // caret position, after we press Enter on Line 1

Pressing Ctrl+Enter will evaluate the current code, but show a "detailed view" of the response. For example, for the same expression below, on the first line we pressed Enter, but on the second line we pressed Ctrl+Enter:

> DateTime.Now // we pressed Enter
[5/30/2021 5:13:00 PM]
> DateTime.Now // we pressed Ctrl+Enter
[5/30/2021 5:13:00 PM] {
  Date: [5/30/2021 12:00:00 AM],
  Day: 30,
  DayOfWeek: Sunday,
  DayOfYear: 150,
  Hour: 17,
  InternalKind: 9223372036854775808,
  InternalTicks: 637579915804530992,
  Kind: Local,
  Millisecond: 453,
  Minute: 13,
  Month: 5,
  Second: 0,
  Ticks: 637579915804530992,
  TimeOfDay: [17:13:00.4530992],
  Year: 2021,
  _dateData: 9860951952659306800
}

A note on semicolons: C# expressions do not require semicolons, but statements do:

> var now = DateTime.Now; // assignment statement, semicolon required

> DateTime.Now.AddDays(8) // expression, we don't need a semicolon
[6/7/2021 5:03:05 PM]

Exploring Code

  • Press F1 when your caret is in a type, method, or property to open its official MSDN documentation.
  • Press Ctrl+F1 to view its source code on https://source.dot.net/.
  • Press Ctrl+Shift+C to copy the current line of code to your clipboard, and Ctrl+v to paste.

For example, pressing F1 or Ctrl+F1 when the caret is in the AddDays function will open the MSDN documentation for AddDays and the source code for AddDays respectively.

> DateTime.Now.AddD|ays(8) // "|" denotes caret position

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

Command Line Configuration

The C# REPL supports multiple configuration flags to control startup, behavior, and appearance:

Usage: csharprepl [OPTIONS] [response-file.rsp] [script-file.csx]

Supported options are:

  • OPTIONS:
    • -r <dll> or --reference <dll>: Add an assembly reference. May be specified multiple times.
    • -u <namespace> or --using <namespace>: Add a using statement. May be specified multiple times.
    • -f <framework> or --framework <framework>: Reference a shared framework. Available shared frameworks depends on the local .NET installation, and can be useful when running a ASP.NET application from the REPL. Example frameworks are, but not limited to:
      • Microsoft.NETCore.App (default)
      • Microsoft.AspNetCore.All
      • Microsoft.AspNetCore.App
      • Microsoft.WindowsDesktop.App
    • -t <theme.json> or --theme <theme.json>: Read a theme file for syntax highlighting. The NO_COLOR standard is supported.
    • -v or --version: Show version number and exit.
    • -h or --help: Show this help and exit.
  • response-file.rsp: A filepath of an .rsp file, containing any of the above command line options.
  • script-file.csx: A filepath of a .csx file, containing lines of C# to evaluate before starting the REPL.