Introduction to C# Interactive

Raymond Raymond event 2020-09-24 visibility 3,529
more_vert
Introduction to C# Interactive

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

2020092443743-image.png

The windows looks like the following screenshot:

2020092443858-image.png

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.

2020092445013-image.png

Full screenshot

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

2020092445105-image.png

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

2020092451103-image.png

2) Type csi

2020092451131-image.png

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:

2020092451804-image.png

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:

2020092452306-image.png

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#!

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts