New C# REPL and scripting capabilities

Overview

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

SNAGHTML1a4442a5

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

image

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.

SNAGHTML1a69c7de

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):

SNAGHTML1a6ff114

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.

SNAGHTML1a73f130

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.

SNAGHTML1a7bc779

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

SNAGHTML1a83c8da

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

SNAGHTML1a854da5

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:

image

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

SNAGHTML1a8f7eb7

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

SNAGHTML1a9116ac

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:

image

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.

SNAGHTML1a9ddc82

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 !

View your Application Insights data in Visual Studio

If you haven’t tried Microsoft Azure Application Insights before, it is a powerful tool for gaining insights in to how your web apps or mobile apps are being used, how they are performing, and what issues they may have.  You can even setup alerts in Application Insights to keep you informed when things happen that you care about.

Until now, you would primarily view the Application Insights data for your application using the Azure Portal (http://portal.azure.com)

SNAGHTML1d8838f

Now with the Visual Studio 2015 Update 1 release though, you can now view Application Insights Data from right within Visual Studio 2015. To do so, from the Visual Studio 2015 menu bar, select “View” | “Other Windows” | “Application Insights Search

SNAGHTML1dbffa6

Then, when prompted select the Application Insights Resource you want to use:

image

Finally, you can use the search an filter criteria to zero in on exactly the Application Insights data you want to see

SNAGHTML1e5e8a3

This new features makes it even easier to gain meaningful insight into your applications.  Give it a try and let me know what you think!