New C# REPL and scripting capabilities


Along with a number of other welcome additions to Visual Studio, Visual Studio 2015 Update 1 brings back and old friend. the C# Interactive Window.  The C# Interactive Window is what’s known as a “Read-Eval-Print-Loop” (REPL).  Interactive Windows are great when you just need to test a simple block of code, and don’t want to create an entire sample project, or code file. 

Opening the C# Interactive Window

Once you have Visual Studio 2015 with Update 1 running, From the menu bar, select “View” | “Other Windows” | “C# Interactive


Once the “C# Interactive” window appears, you can issue a “#help” command to get basic help on the commands available:


For a full set of documentation, you can check out Interactive Window Documentation on GitHub. You should also check out Kasey Uhlenhuth’s “Introducing the Visual Studio ‘C# REPL’” video from the “Connect(); //2015” event.  The documentation and Kasey’s video gives a pretty good introduction to the functionality so I won’t try to repeat that here, however, there are a few extra things I’ve found that I thought I’d share with you.

Give it a Try

If you have the Interactive Window open, you can try just entering a simple expression like “Hello, “ + “World!” and pressing ENTER.  You should see the result “Hello, World!” printed out for you.


Let’s try something with variables (you can use the #cls or #clear commands to clear the interactive window before moving on if you like a clean slate):


If you want to reset the state of the interactive window, including any variable or method declarations, you can use the #reset command.  The reset command clears the state, and reloads the “CSharInteractive.rsp” file to provide a default set of references and namespace imports (“using” directives). For example, here you can see that once I issue the #reset, the hello and world variables are now undefined.


To Semi-Colon or Not To Semi-Colon

When you type an expression (like “Hello, “ + “World!” ) into the window, you never use a semi-colon at the end, because it is an expression, not a statement.  The interactive window will always evaluate expressions, and output the result. 

When working with statements however, they MAY return a result (like in a method that returns a string).  For example, here I will create a method, called Concat that returns a string back to the caller.


Now, I’m going to call the method like an expression, WITHOUT a semi-colon.  Notice that the return value is displayed:


However, if I call it like a statement by adding a semi-colon.  The statement is executed, but the return value isn’t displayed. 


Command Line REPL

Lastly, I’ll mention that there is a command line version of the REPL functionality.  It doesn’t support all of the same commands as the interactive window, notably #clear, #cls and #reset are missing.  For the #clear and #cls commands, you can just use the command prompt’s cls command.  For the #reset, just stop the REPL and restart it. 

You need the Visual Studio tools to be in the path, the easiest way is to just use the “Developer Command Prompt” shortcut installed with Visual Studio:


From there, you can use the “csi.exe” command line REPL utility.  Calling “csi” with a “/?” parameter shows it’s optional parameters:


Here is the command prompt version of our Concat method and calls from before:


You can exit the REPL by typing Ctrl-C.

CSX Scripts

Lastly, you can save your C# scripts into a text file with a .csx extension.  Actually, the extension isn’t really important as long as you reference the full name when call it.  For example, I’ve saved a version of our Concat method declaration and call from above in a text file:


With your CSX scripts, you can’t have loose expressions (like “Hello, “ + “World!”).   All code needs to be wrapped up in methods or be statements.  Also, output is not automatically displayed.  If you want to see something, output it yourself using Console.WriteLine(), etc.


Wrap Up

It’s great to have the C# interactive window back.  I think there are a lot of use cases this will help solve and as a person who does demos a lot I think it will be a handy way to demo small concepts.  Check out the documentation, give it a try, and let me know what you think !