Daily Archives

One Article

Visual Studio Tips

C# Interactive in VS Code Part 1

Posted by matteskolin on

Microsoft introduced the Interactive Window for C# into Visual Studio in Visual Studio 2015. This feature provides a REPL shell that allows you to run c# in a scripting mode.

REPL stands for Read-Evaluate-Print-Loop.

This is useful for things like trying out unfamiliar langugage features, classes, or small snippets of code in a clean environment.

To access the interactive windows in Visual Studio Proper, navigate to

View -> Other Windodws -> C# Interactive


This same functionality can be accessed in VS Code via the dotnet-script command. Note currently this is not included when installing the .NET CLI. It needs to be added as a global tool with the following command.

dotnet tool install --global dotnet-script 

Link to github here

https://github.com/filipw/dotnet-script


To Start a REPL session similar to the C# interactive window in VS Code, run the scripting tool from the VS Code powershell terminal.

dotnet-script

Here we use the REPL to declare and initialize a list of integers and display the sum. C# statements end with a semicolon as normal, but you can also evaluate expressions and the reuslt will be printed as we see in the total below.

PS C:\src\SandBox\CSharpScriptHelpers> dotnet-script
> var sales = new List<int>(){234,235,765,453,234,234,234,222};
> var total = sales.Sum();
> total
2611
>   


The above example was rather, simple. What do we do when we want to script using classes in other assemblies or with more complex initialization?

Using .CSX Scripting Files in Interactive

.csx files are C# scripting files that allow you to run C# code more like a scripting langugage. Code does not have to be in classes or namespaces, although you can use classes as part of the script.

Instead of typing each line individually into the REPL, you can execute an entire block of code at once by loading a .csx file. Unlike normal .cs code files, these files do not require that code be contained in namespaces. Statements can be written outside of functions,

Let’s define our example above in a file called SalesTotal.csx below.

While using a csx file, it is easier to define functions and load other scripts or assemblies. When using just the REPL, if you make a mistake you often have to reset everything and start over. Using a csx file allows you to get your scripting just right before executing it in the C# interpretor.

//SalesTotal.csx

    //define function for calcuating standard deviation
    public static double StdDev(IEnumerable<int> list)
    {
        // ref: https://stackoverflow.com/questions/2253874/linq-equivalent-for-standard-deviation
        // ref: http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/ 
        var mean = 0.0;
        var sum = 0.0;
        var stdDev = 0.0;
        var n = 0;
        foreach (var value in list)
        {
            n++;
            var delta = value - mean;
            mean += delta / n;
            sum += delta * (value - mean);
        }
        if (1 < n)
            stdDev = Math.Sqrt(sum / (n - 1));

        return stdDev; 

    }



 var sales = new List<int>(){234,235,1765,453,234,234,234,222};


 var total = sales.Sum();
 var average = sales.Average();
var stdDev = StdDev(sales);

 
Console.WriteLine($"Total Sales: {total}");
Console.WriteLine($"Sales StdDev: {stdDev}");