Using C# Interactive With Visual Studio 2015

This article explains why C# interactive window is the best Code Snippet Compiler and Execution environment as compared to any other options like online C# pad, C# Code Editor, Online C# Code Compiler, Third Party tools to compile C# code snippet etc,

editor

C# Interactive window is a very useful window which provides us the features to test our code snippet without compiling the application.

Using C# interactive window we can do a lot of things like execute and see your code output by just typing it in C# interactive window. Select your Code inside the Editor and see the output of that code snippet, support C# 6 & C# 7 language features, write Using directive inside it, add a dll reference, call method of newly added dll, Open outside Visual Studio, execute an *.csx file and many more things. Let’s see all those features one by one.

Selecting your code snippet and execute it without compiling the application

I have written the following code in Visual Studio

  1. void Multiply(int x, int y)   
  2. {  
  3.     WriteLine($ "Multiply of {x} and {y} is : {x * y}");   
  4. }  
  5.   
  6. Multiply(10, 25);  

Now select the above code snippet and press the shortcut “Ctrl+E, Ctrl+E”

It will automatically open C# interactive window and compile code snippet as in the following screenshot.

code

Keep history of last executed code snippet

It keeps the history of last executed code. To verify it close the C# interactive window and again press “Ctrl+E, Ctrl+E” to open it. You will find that it contains all those data which we have executed before closing this window.

Reset C# Interactive Window

There are two options to reset C# interactive window

a. Click on Reset icon ( located at top left corner of C# Interactive window )

reset

b. Use the command “#reset”. Refer the below image.

command

Navigate History

We can navigate to history in C# interactive window for next and previous item.

a. Navigate to Previous: to Navigate to previous we use Up Arrow button (History Previous) located at top left of C# interactive window or shortcut: “Alt + Up Arrow”.

window

b. Navigate to Next : We can navigate to next by using Down Arrow button (History Next) located at top left of C# interactive window or shortcut “Alt +Down Arrow”.

window

One of the best thing about C# Interactive window is that it maintains a lot of things in context of history e.g. when we close C# interactive window and reopen it data is not lost. So got the same window with same data which we have executed last time before closing it.

Clear C# Interactive Screen

To clear C# Interactive window screen we can use the clear screen button available at the top left side of the Window. Refer to the below image for Clear screen button location inside C# interactive window.

clear screen

Clearing the screen does not clear the data from history. It just clears the data from UI screen and if you use the history button or just press the shortcuts “Alt + Up Arrow” or “Alt + Down Arrow” you will get all those data from history.

Different ways to Open C# Interactive Window

We can open C# Interactive windows in 3 ways inside Visual Studio.

1. Using the shortcut Key.

We can use shortcut key : “Ctrl+E, Ctrl+E” to open Interactive window,

shortcut

a. If we select some code snippet and then use the shortcut key “Ctrl + E, Ctrl + E” then it will open C# interactive Window and it also executes the selected code snippet.

b. If we do not select any code snippet and just press the shortcut key “Ctrl + E, Ctrl + E” in that case it will not execute any code snippet and just open the C# interactive window.

2. Open From Context Menu

We can open it by right clicking anywhere on Code window and select “Execute in Interactive” or select some code snippet and then right click >>Execute in Interactive,

execute

a. If we select some code snippet and open it from context menu then it will open C# interactive Window and it also executes the selected code snippet.

b. If we do not select any code snippet and just open it from context menu in that case it will not execute any code snippet and just open the C# interactive window.

3. From View Menu

We can also open it from view menu. Go to View Menu, then click C# Interactive,

window

In the above screenshots you have seen multiple ways to open C# Interactive window inside Visual Studio. Currently it is available with Visual Studio 2015 Update 2 and Visual Studio Preview ‘15’. But it is not necessary that you use C# interactive with Visual Studio we can use it without Visual Studio too. This is a tool integrated with Visual Studio. Let’s see how we can use C# interactive outside the visual studio.

Opening C# Interactive Outside Visual Studio

Open Visual Studio 2015 Developer Command Prompt >> type “csi” inside the command prompt to open it as C# interactive. After that you can execute any scripts or do you calculations or whatever other tasks we are performing inside it when open from visual studio same tasks can be performed here also.

code

Supports C# 6 & C# 7

C# interactive window provides the support for C# 6 and C# 7 as well. We do not need to change anything for executing C# code features from C# 1.0 to C# 6.0 but for C# 7.0 feature we need to make a small change to compile it properly.

Change for C# 7

Solution Explorer, Select your Project, Right Click, Select Properties, Go to Build Tab, General, then Conditional compilation symbols:

Enter “__DEMO__ & __DEMO_EXPERIMENTAL__” in the textbox as in the following screenshot,

build

Full support of intellisence

It provides the full support of intelliSense as you can see in the following screenshot.

tod

Statement can be written in multiple lines

C# Interactive window provides the support to write your code snippet statements in multiple lines. If you have copied data from somewhere and just pasting those data inside Interactive window in that case it automatically expand to multiple line if your copied code have multiple lines. But if you are just typing the code in C# interactive window and if you press enter it might evaluate that code but if you want to type it in multiple lines just use the shortcut “Shift + Enter” to start a new line without executing the expression. When you press “Shift + Enter” it automatically adds a dot(.) at start of new line which indicates that statement is continued. You can refer the below screenshot for the same.

code

To test this code snippet,

  1. using static System.Threading.Thread;  
  2.   
  3. List < string > fruits = new List < string > ();  
  4.   
  5. fruits.Add("Apple");  
  6.   
  7. fruits.Add("Banana");  
  8.   
  9. fruits.Add("Grape");  
  10.   
  11. fruits.Add("Guava");  
  12.   
  13. fruits.Add("Mango");  
  14.   
  15. Parallel.ForEach(fruits, fruit =>  
  16.   
  17.     {  
  18.   
  19.         Console.WriteLine  
  20.   
  21.             ($ "Fruit Name: {fruit}, Thread Id= {CurrentThread.ManagedThreadId}");  
  22.   
  23.     }  
  24.   
  25. );   

C# Interactive Commands

Command

Description

Ctrl+A

select current submission inside interactive window

Ctrl+A, Ctrl+A

select the entire content of interactive window

Esc

Clear Submission or Cancel IntelliSence

Ctrl + Space

Trigger IntelliSence

Ctrl + J

List type members

Ctrl + F

Find

Ctrl + H

Find & Replace

Ctrl + Enter

Evaluate the expression

Shift + Enter

New line without executing the expression

Enter

Evaluate the expression

Ctrl + C

Copy

Ctrl + V

Paste

Alt+UpArrow

Navigate to Previous

Alt+DownArrow

Navigate to Next

Ctrl+Alt+UpArrow

Navigate to Previous by Prefix

Using DLL reference Inside C# interactive window

We Use “#r” to include a dll reference. Refer the below screenshot for more details,

more

If you want to practice it for C# 7 feature I recommend you to test it with Visual Studio ‘15’ preview. To know more about Visual Studio ‘15’ you can go through the below link.


Similar Articles