Executing ad hoc C# code snippets
It may happen that you want to test some C# code without having to start the full car, i.e. open up Visual Studio or Visual Studio code. You may even want to test some of the libraries you’re developing to see how a scenario or behavior you are thinking about would react.
Say no more. In this post, I will show you 2 ways to do so. It won’t require you to create a console app or even run your actual application with your code snippet. This will allow you to quickly test your code and scenarios. Note that the techniques below are to produce outputs, not to actually debug your code. For debugging, you will need an IDE that has a debugger integrated.
Jupyter Notebooks
Jupyter is an open-source project that lets you easily combine Markdown text and executable Python source code on one canvas called a notebook1. In 2020, Microsoft released what they called .NET interactive. .NET interactive is a group of CLI tools and APIs that enable users to create interactive experiences across the web, markdown, and notebooks.
This new tool allows us to this run snippets of my favorite language, C#, into in a notebook. Note that this can be a great way to share and document your libraries and platforms for your team.
To be able to create a new .NET interactive you need to following:
- Install the latest Visual Studio Code.
- Install the latest .NET 5 SDK or newer.
- Install the .NET Interactive Notebooks extension from the marketplace.
Once you have the above requirements installed, fire up Visual Studio code and open up the command palette (ctrl + shit + p on Windows or cmd + shift + p on OSX) and search for .NET Interactive: Create a new blank notebook. Once found, select it.
This will activate the extension and install the .NET interactive CLI tool. Once activated and installed, you will be prompted to choose between creating a DIB notebook (Dotnet Interactive noteBook) or a IPYNB notebook (Interactive Python NoteBook).
The ipynb is the common notebook file format for all languages on Jupyter. The DIB format is more axed to .NET / PowerShell. There’s a discussion on GitHub about this. Note that one contributor says that they may or may not keep the DIB format. In the issue, one user notes that the DIB format is cleaner for differences versus the IPNYB format if you plan on putting the file under source control. Choose the format that makes you more comfortable.
Once you select the format, you will be prompted to choose the language to write the notebook in. Select C#.
You are now ready to start putting your snippets.
The thing that you need to not is to reference any libraries, use #r path_to_the_dll
. A tip would be to use the full path to the DLL for consistency.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#r "C:\Users\Dominique\.nuget\packages\newtonsoft.json\13.0.1\lib\netstandard2.0\Newtonsoft.Json.dll" using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.IO; JObject file; using (var stream = File.Open(@"E:\Temp\blog.json", FileMode.Open)) { using (StreamReader sr = new StreamReader(stream)) { using (JsonReader reader = new JsonTextReader(sr)) { var serializer = new Newtonsoft.Json.JsonSerializer(); file = serializer.Deserialize<JObject>(reader); } } } Console.WriteLine(file.ToString()); |
Below is a capture of the results
Visual Studio Interactive console
You can also run code snippets in the interactive console of Visual Studio. In Visual Studio 2019, navigate to View -> Other Windows -> C# interactive.
You can use the #help command to help you with commands to write your script.
Note that the Interactive console by default runs on .NET Framework. To change to .NET core, type #reset core
.
Example
The following is the same example as the .NET interactive notebook above but done in Visual Studio C# interactive.
Other alternatives
There’s a great tool that also has been around for ages that is called LINQPad. Shout out to Thomas Levesque for reminding me about this tool that I forgot about (and that I used extensively back in the days, shame on me).
Happy coding!