How to REPL in C# with CShell
Perhaps the first question should really be ‘what is REPL’? In truth, much like software testing, many developers use it, without knowing that there’s a name for it – or a better way to do it. In this post I will explain what REPL is, how to use it in your development process, and finally, how CShell makes REPLing so much easier.
What is REPL?
REPL is an acronym, which stands for Read – Evaluate – Print – Loop (see? not so complicated). Essentially, this is a cyclic process, where the computer reads and parses input from the user and feeds it into some algorithm or function, evaluates the algorithm, prints out the function’s output, and then returns (loops) back to the read state for more input. This cycle repeats until terminated.
In some ways, you may consider this to be the interactive version of TDD, where you make up the test as you go, running a function either in order to test that it works as expected, or in order to learn how it works, using different inputs to increase your confidence in the code.
In fact, anytime you write a short block of code, often a console application or a unit test, in order to design or test a function – you are – to some extent – REPLing.
To some extent, because just like with test automation, you really want proper tooling, if you want to do this properly, and do this with ease.
You need a REPL environment.
What is a REPL Environment?
A proper REPL environment is first and foremost, an interactive environment. This means that you should be able to simply drop into the environment, type in some code, and have an immediate indication as to whether the code does what you want it to do.
A naïve and cumbersome way to do it, is of course, open Visual Studio, start a new console application, type some code into the Main() function, and run the application. The problem with this approach, is that (a) it takes a lot of time to reach the part where you can type in some code – too much ceremony, and (b) you have to compile the whole application, and start running from scratch every time you want to run. A true REPL environment should let you evaluate each line separately.
Note that Visual Studio has an Interactive Window. Unfortunately, it is for debugging, and can be used only while running code in debug mode, not while designing it.
Until recently, I’ve been using LinqPad. LinqPad is a really great tool, that allows you to run short scripts in C# (and VB.NET and F#). The commercial version (just under $50) adds intellisense, and supports using NuGet packages for references that your .NET script may need. You can also use it to write short programs as well, and create extensions that you can use with all of your scripts. You can fire up the environment very quickly, and just start coding an expression to REPL, or write a script.
I have, however, a few weeks ago, discovered CShell, and have started using it immediately, and have not looked back since.
Enter CShell
CShell – no relation to the similarly named shell that is (not so) commonly used in UNIX and Linux environment, is a free (as in beer), true REPL environment. It is based on the new Roslyn compiler, and has all of the key components that you need:
- True REPL interactive window – in the IW, you can simply type C# code, hit enter and it evaluates, line-by-line. In the IW, each evaluation is remembered, so you can enter multiple lines, each one based on the results of the previous evaluation.
- A scratchpad window for entering C# and script-CS (.csx) scripts. You can run an entire script, or just selected lines. In this mode, of course, each evaluation runs separately.
- CShell can print to a “sink”, in various formats – text, HTML, XML, graphs, etc.
- Intellisense. It simply just works.
- string based evaluation. You can type in something like
Shell.Evaluate(“1+1”);
and it prints the evaluated result. - Add references from the file system or the GAC.
One nice thing that really helps you get started with CShell is the tutorial. When you open the IDE for the first time, the workspace opens up with the Tutorial.csx script file. It walks you through examples of what CShell can do, and how you can/should use it. One run through it, and I felt as comfortable with it as I do with LinqPad, which I’ve been using for years:
If you accidentally (or purposefully – I won’t judge you) closed the tutorial and want to reopen it, you can find it where you installed it (unzipped the binaries, really – it’s an XCopy deployment), under CShell\Templates\Tutorial.csx.
And did I mention that it’s free?
Reference: | How to REPL in C# with CShell from our NCG partner Assaf Stone at the Software and I blog. |