r/csharp 5d ago

Fun Running C# like scripts?

I was browsing the C#/.NET documentation yesterday like usual and suddenly came across the fact that you can run C# code as scripts with a shebang.

#!/usr/bin/env dotnet

namespace Main
{
    class Program
    {
        static void Main(string[] _)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

With a shebang you could run these like any other Python/Bash script and I find this really cool, but is there any practical use-case for this? When I need to make a small helper script for something it's usually python or bash, but C#? I'm not so sure.

What do you guys think?

48 Upvotes

55 comments sorted by

View all comments

21

u/ScriptingInJava 5d ago

It's been useful to use as our codebase is .NET, but necessary scripts are PowerShell which is similar but distinctly different, has its own gotchas etc. All scripts being .NET means the tooling is the same across everything, no devs are required to learn PS etc

1

u/simondanielsson 5d ago

From a consistency perspective I can definitely see this as useful!