New in .NET 10.0 [14]: Starting single C# files on Linux and macOS

Not only on Windows, but also on Linux and macOS, you can translate and start individual C# files directly since .NET 10.0.

listen Print view
.NET traffic sign

(Image: Pincasso / Shutterstock.com)

1 min. read
By
  • Dr. Holger Schwichtenberg

In my previous article in this series on .NET 10, I described how to start a C# file as a file-based app on Windows. This also works on Linux and macOS – even without having to preface it with dotnet.

Der Dotnet-Doktor – Holger Schwichtenberg
Der Dotnet-Doktor – Holger Schwichtenberg

Dr. Holger Schwichtenberg ist technischer Leiter des Expertennetzwerks www.IT-Visions.de, das mit 53 renommierten Experten zahlreiche mittlere und große Unternehmen durch Beratungen und Schulungen sowie bei der Softwareentwicklung unterstützt. Durch seine Auftritte auf zahlreichen nationalen und internationalen Fachkonferenzen sowie mehr als 90 Fachbücher und mehr als 1500 Fachartikel gehört Holger Schwichtenberg zu den bekanntesten Experten für .NET und Webtechniken in Deutschland.

To do this, you use a so-called hash-bang line or shebang line at the beginning of the C# file:

#!/usr/bin/env dotnet

The following command-line command ensures that the file is executable:

chmod +x filename.cs

Videos by heise

Starting is then possible without mentioning "dotnet":

./filename.cs

On Linux and macOS, it is not even necessary for the file to end with .cs:

On Linux, a C# file can be started without mentioning dotnet (Fig. 1).

However, such a direct start of a single C# file, without writing "dotnet" in front of it, is not possible on Windows.

(mack)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.