Andrés Villenas Blog

Passion for programming

Interactive C#

Probably as a DotNet developer, you have worked in a portion of code that you wanted to test separately, so you can check if it will work as you think. Unfortunately, DotNet languages have been known as languages that need compilation prior to be executed, therefore the results are obtained after the project is compiled and run.

However, a set of new features of the DotNet Compiler Platform (codename Roslyn) allow to execute portions of c# code directly in the new compiler. This new interactive compiler (csi.exe) is a REPL (read-eval-print-loop) environment that processes expressions you enter to it, line by line.

There are two ways to use the interactive compiler: 

  • In the command line interpreter (cmd, powershell or cmder), type csi.exe or, 
  • In Visual Studio 2015, go to View, Other Windows, and select C# Interactive, 

The main difference between both is that csi.exe won't add the default references to DotNet libraries and neither "using" directives to the compilation session. It means that you will have, for instance, enter the following statement

using System;

in order to use  

Console.WriteLine()

Visual Studio, in the other hand, will add that for you automatically. 

 

Once you've started, you can test whatever you need. Let's try something more interesting. Let's say you want to remove all the numbers at the end of the strings in a list using regular expressions. There are some alternatives to do this, however, let's do it directly in the compiler.

 

 

Further information about the Interactive C# Window can be found here.

That's it for today.

See you soon in the digital neighborhood!

Add comment

Loading