Using C# Interactive With Visual Studio 2015 - Part Two

This article explains why C# interactive window is the best Code Snippet Compiler & Execution environment as compared to any other options like online C# pad, C# Code Editor, Online C# Code Compiler, Third Party tools to compile C# code snippet etc.
code

C# Interactive window is a very useful window which provides us with the feature to test our code snippet without compiling the application. This is my second article for C# interactive window and I will use some features of Visual Studio ‘15’ preview so it is recommended to go through these two articles,

In last article of C# Interactive I explained about these points.

Select your code snippet and execute it without compiling the application, keep history of last executed code Snippet, Reset C# Interactive Window, Navigate History, Clear C# Interactive Screen.

Different ways to Open C# Interactive Window, Support for C# 6 & C# 7, Full support of intellisence, Statement can be written in multiple lines, C# Interactive Commands, Using DLL reference Inside C #interactive window, etc. Now I will explain about those points which are not covered in last article.

Executing a *.csx file

In C# interactive window you can also run an external file. If you want to run an external file inside it then please make sure that the external file you are using is having valid C# code snippets. To run an external file we use the command #load.

e.g. #load "D:\MyCustomScripts\MyFirstScript.csx".

I have created a file “MyFirstScript.csx” which contains some C# code snippets,

  1. using static System.Console;  
  2. using static System.DateTime;  
  3. using static System.Globalization.CultureInfo;  
  4. WriteLine("Welcome to the session of C# Interactive Window");  
  5. WriteLine($"Today is : {Now.DayOfWeek} and \n Current Time is {Now.TimeOfDay}");  
  6. public void LinqQuery()  
  7. {  
  8.     int[] numberList = { 115, 41, 11, 55, 191, 181, 161, 171, 8, 33,222, 388, 253};  
  9.   
  10.     var oddNumbers =  
  11.         from n in numberList  
  12.         where n % 2!=0  
  13.         select n;  
  14.   
  15.     WriteLine("Odd Numbers in the list are :");  
  16.     foreach (var odd in oddNumbers)  
  17.     {  
  18.       WriteLine($"{ odd:0000}");  
  19.     }  
  20. }  
  21. WriteLine("...");  
  22. WriteLine("...");  
  23. WriteLine("Printing LINQ Result");  
  24. LinqQuery();  
  25. WriteLine("...");  
  26. WriteLine($"Current UI Culure is : {CurrentUICulture}")  
interactive

You may have noticed that I am using a .csx file to execute because .csx is a standard extension used for C# script files but to execute code with C# interactive you can use any other file extensions too.

To test it I have just renamed the file “MyFirstScript.csx” with “MyFirstScript.abc” and executed it from C# interactive windows and it is executed and generated the same output.

#load "D:\MyCustomScripts\MyFirstScript.abc"

So it proves that C# interactive does not file extension but checks for C# code snippet.

Executing .csx from command prompt

We can also execute the external file from Developer command prompt. 
  1. Open “Developer Command Prompt for VS2015”.
  2. Type csi and press enter.
  3. Now type,
    #load "D:\MyCustomScripts\MyFirstScript.abc"

If we are loading any file it does not mean that C# interactive will only display the output but we can use function of loaded file.

Using Functions of a loaded Files

We can use functions of a loaded file inside C# interactive window very easily. To see how it can be used I have created a file “MyCalculator.csx” with the following contents.

  1. public int Add(int x, int y) => x + y;  
  2. public int Subtract(int x, int y) => x - y;  
  3. public int Multiply(int x, int y) => x * y;  
  4. public double Divide(int x, int y) => x - y;  
  5. public int Mod(int x, int y) => x % y;  
Now go to C# interactive window and load this file.

#load "D:\MyCustomScripts\MyCalculator.csx"

Now use some methods of loaded file.

e.g. WriteLine(Add(50, 100)); WriteLine(Mod(34, 7));

Refer the below image for more details.

details

In the above screenshot you can see that I am able to use methods of loaded file and to use it we do not need to create any object for it. I am not using any class or struct keyword inside the file. Just loading files and directly calling method names.

Loading multiple files with C# Interactive and see the list of overloaded methods.

We can load multiple files inside the C# interactive window. And we can use all the method names without any object creation and without using the type name.

Now I am going to load 2 files “My1stScript.csx” & “My2ndScript.csx”

My1stScript.csx
  1. int Add(int x, int y) => x + y;  
  2. int Subtract(int x, int y) => x - y;  
  3. int Multiply(int x, int y) => x * y;  
  4. double Divide(int x, int y) => x - y;  
  5. int Mod(int x, int y) => x % y; 
My2ndScript.csx
  1. int Add(int x, int y, int z) => x + y + z;  
  2. int Subtract(int x, int y) => x - y;  
  3. int Multiply(int x, int y) => x * y;  
  4. double Divide(int x, int y) => x - y;  
  5. int Mod(int x, int y) => x % y; 
You can see that My1stScript.csx is having Add() method with 2 parameters and My2ndScript.csx containing the Add() method with 3 parameters. Now I am going to load both files and then using both Methods.

Methods

In the above screenshot you can see that I have loaded both files and I am able to use both overloaded method and you can see in the IntelliSence it is showing that it has 2 overloaded methods.

Loading 2 files with same method and same parameter but with different logic.

You may be thinking that we are not using the class name and it is automatically deciding which method should be called but consider the scenario when we load 2 files with same method name and with same number of parameter list but with different output. Let see what happen in that case.

Script3.csx
  1. void Show()  
  2. {  
  3.     Console.Write("This is the metods called from the file Script3.csx");  
  4. }  
Script4.csx
  1. void Show()  
  2. {  
  3.     Console.Write("This is the metods called from the file Script4.csx");  
  4. }  
script

In the above screen shot you can see that it is showing two overloaded methods but it is calling the method from the latest loaded file and giving the output from last loaded file.

C# Interactive window also supports feature to search/suggest your file while loading.

window

When we are typing the path with #load it is automatically suggesting the file names.


Similar Articles