Introduction To the C# REPL

The folks at Microsoft have been bringing good news to their developer community. The C# community is no exception, as they released a tool that makes our jobs easier. For users of languages such as Python, Haskell, or F#, this is nothing new, but for those of us who follow the edit-build-compile cycle, this is truly remarkable. I'm speaking about the C# REPL, which shipped with Visual Studio 2015, Update 1. This was possible due to the .NET Compiler Platform, which allows developers to write tools pertaining to the analysis of source code, and the generation of code that can be executed on the Microsoft Common Language Runtime.

To "fire up" the C# REPL, one must open up a developer command prompt. (If desired, the reader can search for this program using the search box of his/her operating system. I prefer to "pin it" to the task bar once I find it.)
Once the command prompt is open, one must type the following:
  • csi
We are now inside of the REPL. The following is a list of commands that can be used for this program:
  • #load
  • #help
This command brings up a short set of instructions to use the REPL effectively.
 
#load "file_path.csx"  This command takes a path to a .csx file, and loads it for later use. (A .csx file is a text file that contains C# code. One can think of this as a script.)
 
#r "file_path.dll"  This command adds an assembly to the program for exploration.

A .csx file can contain any C# code desired. The user of the REPL can also type any desired code on it, instead of using .csx files.

Developers can use the REPL to explore API's, or simply to test code quickly, instead of creating new projects and solutions, only to test a small code segment. Did I mention that one does not have to define the Main() method in order to have a program launched? So, one can bet that Console.WriteLine("Hello world."); will work by itself on the C# REPL..

The C# REPL is a tool that was much anticipated, and one of the most requested; A C# developer can only wonder how he/she was able to survive without it. Also, there is no doubt that other innovations will benefit greatly through and by the use of this handy tool. No matter what the role of a C# developer plays on a software mission, this tool will be used extensively, as it will increase productivity and minimize development and build time.


Similar Articles