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.
(Image: Pincasso / Shutterstock.com)
- 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.
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:
However, such a direct start of a single C# file, without writing "dotnet" in front of it, is not possible on Windows.
(mack)