Use C# Interactive Window for your coding experiment in Visual Studio 2015

Use C# Interactive Window for your coding experiment in Visual Studio 2015

The Interactive Window in Visual Studio is a great utility to play around the code, language features, API’s and other .NET framework features. It is a REPL  (Read–eval–print loop) that provides us an environment for live coding and evaluate expression or code snippet based on our inputs. The Interactive Window also support features like IntelliSense, syntax highlighting and navigating through executed commands.

We can also includes references to external dependency and also using namespaces like a normal Visual Studio Editor features.

To launch the Interactive window navigate through  Views > Other Windows > C# Interactive.

C# Interactive Window
C# Interactive Window

Here is some of the common examples to start with…

Following example shows how we can define a basic variable, and write a basic C# code with the interactive window.

Basic C# Coding with Interactive Window
Basic C# Coding with Interactive Window

You will have full intellisense and tool tip support as similar to our main Visual Studio code editor.

Intellisense support in C# Interactive Window
Intellisense support in C# Interactive Window

You can also use the different language feature like I have used the string interpolation feature here –  Easily format strings – String interpolation in C# 6.0

using Language Features
using Language Features

Now, weather it a simple C# code, complex code or even a LINQ you can execute live within the editor.

Writing Complex, Dynamic, LINQ Code Snippet in C# Interactive Window
Writing Complex, Dynamic, LINQ Code Snippet in C# Interactive Window

And of course you can do declare methods and use it.  You might think what could the use of it ?  Think in a way, you can write your method here, execute and test and finally when it is working fine with expected output you can move it to your main code.

 

Methods in C# Interactive Window
Methods in C# Interactive Window

The Interactive Window also support the using namespace, and once included  then you can leverage all the feature for those assembly.

Using Namespaces
Using Namespaces

This tool also support error handling both compile and runtime as shown in below screenshot

Exception and Error Handling
Exception and Error Handling
DivideByZero Exception
DivideByZero Exception

well, to summarize, the  Interactive window is for quick prototyping, checking some quick output of C# code without creating some new projects.  This is really great utility available with Visual Studio and can help us to do quick code experiment. !

Hope this helps !

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.