Dot Net Script

 

C# scripts using dotnet-script

dotnet-script 1.0.0 released – with support for .NET 5.0 and C# 9

You likely know that open source .NET Core is cross platform and it's super easy to do "Hello World" and start writing some code.

You just install .NET Core, then "dotnet new console" which will generate a project file and basic app, then "dotnet run" will compile and run your app? The 'new' command will create all the supporting code, obj, and bin folders, etc. When you do "dotnet run" it actually is a combination of "dotnet build" and "dotnet exec whatever.dll."

What could be easier?

C:\Users\lokesh\Desktop\scripts> dotnet tool install -g dotnet-script
You can invoke the tool using the following command: dotnet-script
C:\Users\lokesh\Desktop\scripts>copy con helloworld.csx
Console.WriteLine("Hello world!");

        1 file(s) copied.
C:\Users\scott\Desktop\scriptie>dotnet script helloworld.csx
Hello world!

Pretty cool yeah? If you were doing this in Linux or OSX you'll need to include a "shebang" as the first line of the script. This is a standard thing for scripting files like bash, python, etc.

Console.WriteLine("Hello world");

This lets the operating system know what scripting engine handles this file.

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.

Installation

The installation command is the same as in previous versions – you can get ahold of dotnet-script by running the dotnet tool installation:

For existing users, the update command is relevant:

If you are using some of the dotnet-script libraries in your projects, those are, obviously, also available on Nuget.

You can find the release notes for 1.0.0 here. Due to some last second (zero day release!) unforeseen compatibility issues, we ended up quickly releasing 1.0.1 with some small tweaks already as well.

The main purpose of the release is to give you a tool that is fully compatible with the 5.0 SDK already and – naturally – to enable C# 9 support! For example, the following script using C# records is now valid:

This applies to both scripting mode and the interactive shell.


Comments

Popular posts from this blog

Using C#9 record and init property in your .NET Framework 4.x, .NET Standard and .NET Core projects

Easy Steps to migrate from Telligent CS to SP Online

How to create API documentation