Sometime ago I posted an article about Dotnet Try. This is a wonderful tool to document your code but, since then, a lot has changed.
.NET 6 and C#10 are here, Visual Studio Code is a nice environment for editing your code, and you can even edit your code in the web. And a new feature has appeared to document your code: .NET Interactive. With it, you can create interactive notebooks and create interactive pieces of code, where you can share and try sample code.
Its use is very simple: just install the .NET Interactive extension in VS Code and you’re ready to go. Just press Ctrl+Shift+Alt+N and open a new notebook (you can choose C#, F# or even Powershell as the default language) and a new notebook is ready to use. If you want to open an existing notebook, just press Ctrl+Shift+P and select .NET Interactive: Open Notebook and open the existing notebook.
When you create a new notebook, you can add markdown or code cells. The markdown cells have text formatted with the Markdown syntax and the code cells can have code, in the selected language. You can run the code by clicking the arrow icon next to the cell or by pressing the Ctrl+Alt+Enter keys.
To display the values, you can use
Console.WriteLine or simply declare a variable and put it in a single line like in
if you want to display more than a single value, you can use the display function, like in
You can even mix languages in the notebook. If you add !#fsharp in the first line, you can add F# code, like in:
Getting data from an URL
You can get some data from an URL, using the command
#!value --from-url https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.csv --name housingPath
Once you have the data, you can display it with SandDance, with this code:
using Microsoft.Data.Analysis; using Microsoft.ML; using System.Collections.Generic; var housingData = DataFrame.LoadCsv(housingPath); housingData.ExploreWithSandDance().Display();
Using Nuget Packages
You can also use Nuget packages with the #r directive. For example, we can get the suppliers dataset as a Json file from the url “https://northwind.vercel.app/api/suppliers”, with this code:
#!value --from-url https://northwind.vercel.app/api/suppliers --name suppliersJson
Then we can show the data as C# classes with:
#!share --from value suppliersJson #r "nuget:Newtonsoft.Json" using Newtonsoft.Json; public record Address(string Street, string City, string Region, string PostalCode, string Country, string Phone); public record Supplier(string Id, string CompanyName, string ContactName, string ContactTitle, Address Address); var suppliers = JsonConvert.DeserializeObject<List<Supplier>>(suppliersJson); suppliers
As you can see there are multiple possibilities. I even created the notebook for this article (you can download it at https://github.com/bsonnino/CSharpNotebooks) based on my last article, about Linq in .NET 6. You can use it as a notebook for you, as a learning tool, as a prototype tool, or even as a tool for sharing knowledge.
The notebook for this article is at https://github.com/bsonnino/CSharpNotebooks