Bhavya

Code -> Break -> Fix -> Blog

Visual Studio : C# Interactive Window

1 Comment

Recently I came across C# (comeback) feature, Interactive Window, which comes with Visual Studio 2015 Update 1. The interactive Window is a REPL which executes the program piecewise.

You will no longer have to create small ConsoleApplication projects just to try out a small piece of code (bye bye ConsoleApplication42…). With Interactive Window, developers can get output for small code snippets without any project creation,  code file or any assembly. Just type in the code and get the result.

C# Interactive Window is part of Visual Studio which makes it easy to learn new language features and experiment with .NET technologies similar to CShell (not completely) but within Visual Studio.

To open Interactive Window, got to View -> Other Windows and select C# Interactive. As shown below.

Navigate.png

Helpful REPL commands to remember before you start:

#help : Display help on specified command, or all available commands and key bindings if none specified.

#clear/#cls : Clears the contents of the editor window, leaving history and execution context intact.

#reset : Reset the execution environment to the initial state, keep history.

As always lets start with Hello World example:

Hello World 1

You can see in the above code, we created 2 strings variables str1 & str2 and wrote the output to the console. Important things to note here, 1. A ‘;’ will make the statement part of the execution context. 2. Pressing ‘Enter’ after the statement without ‘;’ executes the context. Like here it displays the output ‘Hello World’. 3. Even after we got the output the execution context is still intact. So if I execute Console.WriteLine(str1 + str2), the output will be the same. Use #reset command to reset the execution context. 4. No need to specify the namespace.

I can also create a method in the execution context, and use it later. like

Method.PNG

I created a method HelloName(string name) and calling it in the same context gets me the desired output but when I reset the execution context and call the method again, an error is displayed stating “The name ‘HelloName’ does not exist in the current context”.

You can also save your script in a file on the disk and can execute it via VS Command Prompt (extension is not important).

Script 1

Script 2

How to reference a DLL and use it
You use the #r directive to reference a DLL.
I have created a simple class library named SimpleMath.dll for easy learning:

Class Library.PNG

Using the #r directive, I can reference my SimpleMath.dll in the Interactive Window’s execution context.
ClassLibrary Reference.PNG

Documentation : https://github.com/dotnet/roslyn/wiki/interactive-window
Hope this helps.

Let me know you comments below.

~BS

 

Advertisements

One thought on “Visual Studio : C# Interactive Window

  1. Nice article…to the point…Without too much of details in length….

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s