Recently, I was evaluating the Azure Functions, I observed that all the code you write saved in the format of csx. Initially, I didn’t pay the attention to the extension, later when I want to add more method and looking for some kind of better user interface to write the code instead of the azure portal. When I was searching for this, I found that Visual Studio 2015 (update 1) have the interactive session window where you can write the code and test it.
What is C# scripts?
C# scripts is not newly released; it was there from 2011. Recently, it was officially released as part of Visual Studio 2015 update 1. It is more or less bringing C# into the command line interactive window. It’s a lightweight process to quickly the .NET APIs. One of the important use case for using this C# scripting is to quickly test out the C# code without having a heavy unit testing project solutions. Another example, when you write an Azure Function we can quickly write a C# script code in Visual studio interactive session and I can easily copy the code into the Azure functions.
How and Where to Write the C# scripting code?
For Microsoft Developer, if I say scripting, then it means “PowerShell” for them. Need to remember that C# scripting is nothing to do with PowerShell or either nor to replace the PowerShell. The only objective is to bring the C# into command line interface and use it for consuming the .NET API without writing a class and creating the Visual Studio projects.
We can write the C# script code in the two ways, either using the command line window. Or using the Visual Studio C# interactive window.
For command Line, open the command prompt and navigate to the “C:\Program Files (x86) \MS Build\14.0\Bin”; Then type CSI. The scripting session will be started and developer can write the C# code in the command line.
Now developer can write the C# code in command line.
Example 1: concat the two strings.
Example 2: Play around with System variables
Example 3: Create the Method and use it
Example 4: Call the method from the C# Application
Example 5: Call the method from the script resource.
Visual Studio Interactive C# window
In general, Microsoft developers used the VS as the development tool. Developers can also use the interactive window C# for the developing the scripts.
This is not same as PowerShell script. So cls, quit, exit those commands don’t work on the C# command line window.
1. To clear the window – use the Console. Clear ()
2. To evaluate the expression, use the commands as expression instead of statement. For example, if you want to evaluate, don’t finish the command with statement. Refer the example 5. Closing an expression hides the output in command line window.
3. If developers looking for the intellisense, then it’s better to use the Visual Studio 2015 C# interactive window.