During my C# development sessions whenever I need to understand an API call or I need to quickly triage an idea, I usually end up creating a test harness C# Console Application project. Then I step through the code in the debugger, watch the locals windows and most likely experiment in the Immediate Window. This process of triaging and experimenting with the language/API is bit drawn out and in my opinion unproductive.
Looking a bit further afield, Lisp programmers have always enjoyed a tool called REPL (Read-eval-print-loop). The idea being that REPL would read your Lisp code, evaluate it (i.e. compile and run the code) and print the result back to the console window. All these without having to go through separate steps of having to create a test harness project, compiling the code and finally executing the code. This low ceremony approach makes software development process immensely productive and greatly facilitates iterative style of development.
C# developers looking for similar experience were left for wanting until recently. A new toolset called Roslyn from Microsoft seeks to address that need by providing a tool called C# Interactive. Roslyn as a tool provides other useful functionalities but the scope of this blog focuses on the use of Interactive tool for C#.
Getting started with Roslyn is easy. Head off to http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx and download Microsoft “Roslyn” September 2012 CTP. The site also provides a walk-through to get started using the Interactive tool. The link is http://go.microsoft.com/fwlink/?LinkID=263980.
Here’s a quick summary of features provided by this tool.
- Select a region of code to execute in the Interactive.
- Syntax highlighting.
Here are the screenshots of the C# Interactive tool in action. Basically I have defined a class HelloWorld with method SayHello() in the VS editor, selected the definition, right-click and chose “Execute in Interactive”. Once executed, I now create an instance of HelloWorld and invoke the SayHello() method and see its result.
C# Interactive is definitely a very useful tool and will only serve to enhance C# software development experience. With comparison to the Lisp REPL – the automatic printing of code evaluation seems to be missing at this stage.