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.
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:
dotnet 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.
mkdir console
cd console
dotnet script init hello
dotnet script hello.csx
.csx
(C# script) file along with an omnisharp.json
file for intellisense and a VS Code launch.json
file for debug support. dotnet script filename
.The generated hello.csx
file has the following contents:
#!/usr/bin/env dotnet-script
Console.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
.)
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:
dotnet script init stateless
.code .
.#r
directive.Cmd+.
(or Ctrl+.
).Here's my stateless.csx
file:
#!/usr/bin/env dotnet-script
#r "nuget: Stateless, 4.2.1"
// Copied from: https://github.com/dotnet-state-machine/stateless/blob/dev/example/OnOffExample/Program.cs
using Stateless;
const string on = "On";
const string off = "Off";
const char space = ' ';
// Instantiate a new state machine in the 'off' state
var onOffSwitch = new StateMachine<string, char>(off);
// Configure state machine with the Configure method, supplying the state to be configured as a parameter
onOffSwitch.Configure(off).Permit(space, on);
onOffSwitch.Configure(on).Permit(space, off);
Console.WriteLine("Press <space> to toggle the switch. Any other key will exit the program.");
while (true)
{
Console.WriteLine("Switch is in state: " + onOffSwitch.State);
var pressed = Console.ReadKey(true).KeyChar;
// Check if user wants to exit
if (pressed != space) break;
// Use the Fire method with the trigger as payload to supply the state machine with an event.
// The state machine will react according to its configuration.
onOffSwitch.Fire(pressed);
}
Notes for those new to developing on VS Code:
Cmd+Shift+P
--> Omnisharp: Restart Omnisharp
. See filipw/dotnet-script#424 for more information."console": "integratedTerminal"
property to .vscode/launch.json
. More information here.That's it. If you ask me I think it's pretty slick.
What I love about dotnet-script
is:
public static void main
or .csproj
files.