C# .NET Interactive

Introduction

C# is a widely-used programming language developed by Microsoft. It is commonly used for developing Windows applications, web applications, and backend services. .NET is a framework developed by Microsoft that provides a runtime environment for executing applications written in various programming languages, including C#. .NET Interactive is an open-source project that enables interactive coding and data exploration in C# using Jupyter notebooks.

In this article, we will explore the features and benefits of C# .NET Interactive and provide code examples to illustrate its usage.

Features

Notebook Experience

C# .NET Interactive provides a notebook-based experience for writing and executing C# code. Notebooks are a popular tool for data scientists and developers to create and share interactive documents that contain code, visualizations, and narrative text. With .NET Interactive, you can write and execute C# code in a notebook environment, making it easy to experiment and iterate on your code.

Code Execution

.NET Interactive allows you to execute C# code cells individually or as a whole notebook. This enables you to run code snippets and see the results immediately. You can also define variables and functions in one cell and use them in subsequent cells, allowing for a more interactive and exploratory coding experience.

// Define a variable
var name = "John";

// Print the variable
Console.WriteLine(name);

Rich Output

.NET Interactive supports rich output, which means you can display formatted text, tables, charts, and plots directly in your notebook. This is particularly useful for data analysis and visualization tasks. You can use libraries like Plotly.NET or XPlot.Plotly to create interactive charts and plots in your C# notebooks.

#r "nuget: Plotly.NET, 2.0.0-preview.4"
using Plotly.NET;

var data = new[]
{
    new Graph.Bar
    {
        x = new[] { "A", "B", "C" },
        y = new[] { 1, 3, 2 }
    }
};

var layout = new Layout.Layout
{
    title = "Bar Chart"
};

Chart.Plot(data, layout);

Integration with .NET Ecosystem

.NET Interactive seamlessly integrates with the .NET ecosystem, allowing you to leverage existing libraries and frameworks. You can import NuGet packages and use them in your C# notebooks. This enables you to take advantage of the vast collection of libraries available in the .NET ecosystem, such as ML.NET for machine learning tasks or Entity Framework for database operations.

#r "nuget: ML.NET, 1.5.0"
using Microsoft.ML;

var mlContext = new MLContext();

// Create a new empty data view
var dataView = mlContext.Data.LoadFromEnumerable(new List<DataPoint>());

// Define a pipeline
var pipeline = mlContext.Transforms.CopyColumns("Label", "Result")
    .Append(mlContext.Transforms.NormalizeMinMax("Features"))
    .Append(mlContext.Transforms.Concatenate("Features"))
    .Append(mlContext.Transforms.Conversion.MapValueToKey("Label"))
    .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

// Train the model
var model = pipeline.Fit(dataView);

Conclusion

C# .NET Interactive provides a powerful and interactive coding experience for C# developers. It allows you to write and execute C# code in a notebook environment, with support for rich output and seamless integration with the .NET ecosystem. Whether you are a data scientist, developer, or hobbyist, C# .NET Interactive can be a valuable tool for exploring data, prototyping algorithms, and sharing interactive code documents.

Give it a try and start exploring the possibilities of interactive C# coding with .NET Interactive!