C# scripts using dotnet-script

You know how the python folks can just write some code in a .py file and run it with python3 filename.py, without going through the “new project” and the “public static void main” ceremony that C# users have to? C# can write scripts too – something I learnt just like a year back.

Roslyn made C# scripts possible. There’s a Jan 2016 MSDN blogpost by Mark Michaelis titled C# Scripting if you’re interested.

The tool that I’ve been really fascinated by is dotnet-script, which is a cross-platform .NET Core global tool with full intellisense support on VS Code via omnisharp and covers most use-cases for your experimenting needs.

Prerequisites

  1. .NET Core SDK 2.1+
  2. VS Code
  3. Official C# Extension for VS Code

Installation

First make sure you have the latest version of .NET Core SDK installed from dot.net. The minimum requirement is .NET Core 2.1.

The easiest way to install dotnet-script is installing it as a global tool:

1dotnet tool install --global dotnet-script

You should now be able to run dotnet script --version and it should print the version of the dotnet-script tool.

Hello world

  1. Create a new directory to store all your scripts.
  2. Init a new script.
  3. Run.
1mkdir console
2cd console
3dotnet script init hello
4dotnet script hello.csx

The generated hello.csx file has the following contents:

1#!/usr/bin/env dotnet-script
2
3Console.WriteLine("Hello world!");

The first line is a shebang that *nix users must be familiar with. It means we can run it as ./hello.csx and it’ll just work! (Also works on windows, but you’ll need to associate .csx files with dotnet-script first using dotnet script register.)

Another quick example

A typical use-case for console applications is to experiment with new libraries, and dotnet-script is the perfect tool for the job. Let’s say I’m learning about the stateless library. Here’s how I’d go about experimenting:

  1. Create a new script using dotnet script init stateless.
  2. Open the folder in VS Code using code ..
  3. Import the nuget package using the roslyn #r directive.
  4. Go to their GitHub page and copy some sample code over.
  5. Add using statements if needed using Cmd+. (or Ctrl+.).
  6. Debug within VS code with F5.

Here’s my stateless.csx file:

 1#!/usr/bin/env dotnet-script
 2#r "nuget: Stateless, 4.2.1"
 3
 4// Copied from: https://github.com/dotnet-state-machine/stateless/blob/dev/example/OnOffExample/Program.cs
 5using Stateless;
 6
 7const string on = "On";
 8const string off = "Off";
 9const char space = ' ';
10
11// Instantiate a new state machine in the 'off' state
12var onOffSwitch = new StateMachine(off);
13
14// Configure state machine with the Configure method, supplying the state to be configured as a parameter
15onOffSwitch.Configure(off).Permit(space, on);
16onOffSwitch.Configure(on).Permit(space, off);
17
18Console.WriteLine("Press  to toggle the switch. Any other key will exit the program.");
19
20while (true)
21{
22    Console.WriteLine("Switch is in state: " + onOffSwitch.State);
23    var pressed = Console.ReadKey(true).KeyChar;
24    
25    // Check if user wants to exit
26    if (pressed != space) break;
27
28    // Use the Fire method with the trigger as payload to supply the state machine with an event.
29    // The state machine will react according to its configuration.
30    onOffSwitch.Fire(pressed);
31}

Notes for those new to developing on VS Code:

That’s it. If you ask me I think it’s pretty slick.


What I love about dotnet-script is:

  1. All relevant code, including required nuget packages, can exist in a single file.
  2. Full and reliable intellisense.
  3. Debugger support.
  4. No public static void main or .csproj files.

<< Previous Post

|

Next Post >>

#.NET #Dev.to #C#