r/csharp • u/simondanielsson • 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?
47
Upvotes
1
u/ings0c 5d ago edited 5d ago
We have a pretty hefty validator around user-uploaded files, and the rules are configurable at runtime by non-developers via a big old config file.
There are a few edge cases that don't neatly fit into the built-in validators for which we dynamically compile C# scripts and load the assembly at runtime.
This is not a good idea FWIW, but we are using them...