What is C# Interactive all about?

19 februari 2021 om 10:00 by ParTech Media - Post a comment

After the 1st release of 2015 update of Visual Studio, C# interactive was introduced. Today, we can easily understand & implement both API and code snippets of C#, thanks to the interactive window.

In this post, we are going to understand what is C# Interactive and how it became a useful addition to the arsenal of C# developers.

Table of Contents

  1. What is C# Interactive?
  2. Features of C# Interactive
  3. How to work with C# Interactive?
  4. Conclusion

What is C# Interactive?

C# Interactive is an advanced Read- Eval- Print- Loop (REPL) editor. We can use this window for code testing. This window gives us the flexibility of testing programs without compiling. Hence, C# interactive is better than any other alternatives like C# code editor, C# pad and Online C# compiler for executing.

A lot of things get done by utilizing C# interactive. We can execute and view our code simultaneously in it. Here we only need to type it in the window. We can also see the output of any code snippet by selecting the snippet.

Features of C# Interactive

  • C# interactive window supports the languages of C# 6 and C# 7.
  • We can use the directive to write inside it and include .dll files as reference.
  • We can call the methods of recently added .dlls.
  • C# interactive gives us the flexibility to open codes outside Visual Studio.
  • It also allows developers to execute and run *.csx extensions. C# interactive gives developers the flexibility to cut and paste codes between C# interactive and *.csx files. It makes it easier for a developer to execute complex code snippets. It also helps in saving their work.
  • We can create executable test cases. We can run those either from the command line or Visual Studio.
  • Error handling in both runtime & compilation time is supported by C# interactive.
  • C# interactive tracks the history of recently executed code. We can check it by closing C# interactive and use Ctrl or Cmd +E to open. Here we can observe that the window contains all the instructions that get compiled before closing.

How to work with C# Interactive?

We can use this feature by loading Visual Studio 2015 Developer Command Prompt in the command line and execute csi.exe.

We also can use Visual Studio to open this from the view menu.

View> Other Windows > C# Interactive

How to execute a C# Code?

Here a string variable gets initialized and executed it to generate the output.

> string name = “Ruud Partech”;

\> Console.WriteLine(“Hi” + name);

Output: Hi Ruud Partech

Here are some advanced features which we can use in the interactive window to easily write codes:

  • CodeDom
  • Collections
  • ComponentModel
  • Configuration
  • Deployment
  • Diagnostics
  • Dynamic
  • Globalization
  • IO

As discussed earlier we can easily execute any block of code in C# interactive. Here we just need to select the method that we are going to execute. We just have to select the block of code and then right-click. After that, when a dialogue box opens select Execute in Interactive option. To test a method you can simply pass the value of the parameter(s)

How to start a new session of C# Interactive in JetBrains Rider?

We need to select blocks of code from the editor and use the shortcut Ctrl or Cmd + . We can use an alternate method by pressing Alt+Enter and choosing to Send selection to C# interactive to start a new session. The code will append automatically if the instance is already running.

We can also select tools from the menu and then select Start New C# Interactive Session. Here Rider will open a blank C# interactive window and erase the currently running instance by resetting. Developers can also reset the running session by clicking the Reset button from the toolbar.

Developers can reference & select their projects to start a session. Here, they need to browse the project from the Explorer Window. The can select the code and right click mouse button to select the Reference in C# Interactive from the context menu.

The behaviour of C# Code in Interactive

The behaviour of C# code is different in C# interactive. These nuances can be very confusing, and it can annoy developers. The default modifier of C# interactive is public modifiers for all members and top-level variables. This behaviour is different from the typical static compiled code. We can discover that every code asynchronously executes at interactive. There are some modifications needed to move static code in C# interactive.

Demonstrating Value in C# interactive

We need to develop an end to end process to demonstrate values in C# interactive. We can get the outline of a process using this feature. Here we can predict the workflow of a process before the final execution. Hence, it is impactful for delivering the code to customers. We can easily use it to make prototypes for determining the optimal solution.

How to save your progress?

We can save our codes using C# interactive feature. Here we have to create a *.csx file. Visual Studio does not give us the flexibility to save codes directly in *.csx extension. So here we need to add a file and manually exchange the extension to *.csx.

How to Load your code?

We can edit, modify and complete the codes that we have saved earlier. We can use load command to load a program in the C# interactive and continue with the execution. The Command works as follows:

#load “path\filename.csx”

Conclusion

Scripting languages like R & Python support interactivity in their respective integrated development environments (IDE). At the initial phase, developers need to compile C# codes into MSIL. After that, it can be executed in .NET runtime environments. Thanks to C# interactive, programmers now can execute C# codes from Visual Studio directly. They can also execute their C# scripts from other C# interactive supported environments.