While writing a C# program we usually open an application (e.g. Console, Class Library, MVC, WPF etc.) and start writing the code. But there is something call as C# Interactive Window avalabe from VS 2015 that allow us to write C# program without any application. It's a C# REPL window and hosted within Visual Studio. In this article we will look into how to use that.
Introduction
While writing a C# program we usually open an application (e.g. Console, Class Library, MVC, WPF etc.) and start writing the code. But there is something call as C# Interactive Window availabe from VS 2015 that allow us to write C# program without any application. It's a C# REPL window and hosted within Visual Studio. In this article we will look into how to use that.
Straight to Exploration
Open Visual Studio 2015. From the View Menu, Choose Other Windows > C# Interactive.
On clicking that, the below window appears.
The same can be bring into picture by invoking the Quick Launch (press Ctrl + Q) and typing
C# Interactive
We can write the same C# code here too and the IntelliSense work fine. Also it includes syntax color coding
We have written a simple program and it works
Even more complex program can be written as under (like Display Matrix elements)
We can use Linq/Lambda as under
If we want to clear the console window, we can either click on the Clear Screen icon
OR right click -> Clear Screen.
We can go to the Previous history by clicking on the History Previous (Alt+Up Arrow) button
Since C# Interactive window is an entirely independent C# session, so this won't hold good for a new session.
We might have observed , the interactive window displays a message "Loading context from 'CSharpInteractive.rsp'.".
It loads its default execution context from C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PrivateAssemblies\CSharpInteractive.rsp. Opening the CSharpInteractive.rsp file in a text editor displays
/r:System
/r:System.Core
/r:Microsoft.CSharp
/u:System
/u:System.IO
/u:System.Collections.Generic
/u:System.Diagnostics
/u:System.Dynamic
/u:System.Linq
/u:System.Linq.Expressions
/u:System.Text
/u:System.Threading.Tasks
Conclusion
C# Interactive Window brings the scripting and REPL environment to the developers. We can now code C# in an interactive mode without the need of opening an application. Hope this will be helpful. Thanks for reading.