Introduction to C# Interactive

access_time 2 months ago visibility168 comment 0

Python, R and many other scripting languages generally support interactive programming features in their IDEs. When C# was created initially, all C# written programs need to be complied into MSIL first before it can run in .NET runtime environments (unless the code is dynamically complied). From Visual Studio 2015 Update 1, C# Interactive is added back into the IDE and developers can run C# scripts directly with Visual Studio or other environments that support C# interactive programming. 

C# Interactive window in Visual Studio

C# Interactive window can be found in Visual Studio via View -> Other Windows -> C# Interactive

The windows looks like the following screenshot:

Hello world!

Let's complete the classic 'Hello world!' example using this interactive window:

> var helloWorld = "Hello world!";
> Print(helloWorld);
"Hello world!"

Import namespaces

> using System.Diagnostics;
> Console.WriteLine(helloWorld);
Hello world!

Define a function

The following code snippet creates a function named testFunc. Then the function is invoked with two parameters. 

> int testFunc(int a, int b)
. {
.     return a + b;
. }
> var c = testFunc(1, 2);
> Print(c);
3

Create a class

Use the following code snippet to create a class:

> class TestClass { 
.     public int A { get; set; }
.     public int B { get; set; }
. }
> var testClass = new TestClass { A = 1, B = 2 };
> Print(testClass.A);
1
> Print(testClass.B);
2

IntelliSense

When input code, all the Visual Studio IntelliSense are available in the interactive window to improve coding productivities.

Full screenshot

The following screenshot shows the all the code snippets used in the preceding sections:


Program csi.exe

All the magic happens as part of Roslyn project. Roslyn provides open-source C# and Visual Basic compilers with rich code analysis APIs. It enables building code analysis tools with the same APIs that are used by Visual Studio.

In my machine, csi is available in the following location (as part of my VS 2019 installation):

C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/bin/Roslyn/csi.exe

Refer to GitHub repository for more details: https://github.com/dotnet/roslyn/tree/master/src/Scripting.

Run csi.exe

1) Open Developer Command Prompt for VS 2019


2) Type csi

3) Now you can directly use this Command Prompt window to run your C# code.

Helps for csi

When typing '#help' in the interactive windows, all the help information will be displayed:

> #help
Keyboard shortcuts:
  Enter         If the current submission appears to be complete, evaluate it.  Otherwise, insert a new line.
  Escape        Clear the current submission.
  UpArrow       Replace the current submission with a previous submission.
  DownArrow     Replace the current submission with a subsequent submission (after having previously navigated backwards).
  Ctrl-C        Exit the REPL.
REPL commands:
  #help         Display help on available commands and key bindings.
Script directives:
  #r            Add a metadata reference to specified assembly and all its dependencies, e.g. #r "myLib.dll".
  #load         Load specified script file and execute it, e.g. #load "myScript.csx".

C# script file

We can also a C# script text file csharp.csx with the above code:

using System.Diagnostics;
var helloWorld = "Hello world!";
Console.WriteLine(helloWorld);
int testFunc(int a, int b)
{
return a + b;
}
var c = testFunc(1, 2);
Print(c);
class TestClass { 
	public int A { get; set; }
	public int B { get; set; }
}
var testClass = new TestClass { A = 1, B = 2 };
Print(testClass.A);
Print(testClass.B); 

Run the script file

Executable csi.exe can be used to execute C# script file. 

csi csharp.csx

The output looks like the following screenshot:

Use #load directive

In the interactive window, #load directive can be used to load a script file and then run the script:

#load "csharp.csx"

The output looks like the following screenshot:


C# interactive programming in Jupyter Notebook

As part of .NET for Spark project, we can now also use C# as programming language for Spark and you can run C# in Jupyter Notebook too.


Refer to the following articles for more details and examples:

Enjoy scripting with C#!

info Last modified by Raymond at 2 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Want to publish your article on Kontext?

Learn more

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.


Learn more arrow_forward

More from Kontext

Run .NET 5 on Azure App Services

local_offer .NET local_offer Azure local_offer asp.net core

visibility 10
thumb_up 0
access_time 6 days ago

.NET 5 RC2 was released on 2020-10-13. On Azure, you can only select .NET Core 3.1 or 2.1 LTS versions as runtime stack when creating web app as the following screenshot shows.   This will stay as is till .NET 5 official release. However, you can use extensions to run your .NET 5 ...

C# 9.0 New Features

local_offer C# local_offer .NET

visibility 121
thumb_up 0
access_time 2 months ago

.NET 5.0 release candidate 1 (rc.1) was published on 2020-09-14, which marks another big step towards the official .NET 5.0 release. As part of 5.0, C# 9.0 will be released with a bunch of new features. This article summarizes some of the new features with examples. Download .NET 5.0 SDK from this ...

C# 9.0 New Features

local_offer C# local_offer .NET

visibility 121
thumb_up 0
access_time 2 months ago

.NET 5.0 release candidate 1 (rc.1) was published on 2020-09-14, which marks another big step towards the official .NET 5.0 release. As part of 5.0, C# 9.0 will be released with a bunch of new features. This article summarizes some of the new features with examples. Download .NET 5.0 SDK from this ...

About column

About C# Programming Language: features, syntax, usages and so on.

rss_feed Subscribe RSS