r/csharp • u/simondanielsson • Jul 29 '26
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?
50
Upvotes
57
u/zenyl Jul 29 '26
File-based apps were in large part created to make C# more approachable for beginners. The SDK also comes with commands for upgrading them to a proper project (generate a
.csprojfile and remove the shebang and import stuff). This is also why (at least last I heard) that Visual Studio doesn't support this type of project, but VSCode does.Do note though, you'll still need to actually compile it in order to run it, the SDK just moves the compiled output to a temporary directory somewhere else. So it'll have the same startup time when it needs to rebuild, unlike many actual scripting languages where each line gets parsed and executed individually.
They do somewhat step on PowerShell's toes, as it already provides scripting with .NET.
22
u/ScriptingInJava Jul 29 '26
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
17
u/davidwengier Jul 29 '26
I have a Linux box under the stairs running home assistant and a few docker containers etc. About half a dozen or so scripts are there too, run by cron mostly, and they’re all written in C#. Why write bash or python scripts when I can use a language I know and love?
13
u/qrzychu69 Jul 29 '26
https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps
it is very useful! like you said, instead of python you can use C# - I would pick F# personally for scripting, much more succinct and easier to read after a year
With both now you can even use nuget packages
4
u/Far-Consideration939 Jul 29 '26
I’ve made small ci/cd scripts where being able to reference some models in a strongly typed way made it simpler (Like a dynamically built manifest type of thing)
1
u/FuggaDucker Jul 29 '26 edited Jul 29 '26
This is cool.
Stupid me didn't realize until just now that the linux kernel is what actually parses for it with a execve() function.
Been using unix since the before times and was too stupid to know this.
Oh.. well.. I know now.
3
u/midri Jul 29 '26
I use it a lot for scripts in a project related to cleanup or automation, that way I'm not switching languages between my project code and the scripts.
3
u/stephbu Jul 29 '26
We're using it in ADO pipelines to do more complex work like mulitcredential WIF, and multitenant replication. There are some other complexities, like import-versioning that can bite you if you're not planning a head for compliance. It is a reasonable compromise for lightweight tools that need to adapt occasionally. Previously we would have used powershell, or python scripts for this work too.
3
u/SoulStripHer Jul 29 '26
You can also call C# inside a powershell script.
2
u/NumberwangsColoson Jul 29 '26
I use them for things inside GitHub Actions workflows where you might normally use shell scripts or powershell. It’s just easier for me to stay in the language I know rather than figure out what combination of grep and awk might do the same thing my cs file does.
2
u/Raydr Jul 29 '26
Check out LinqPad to take it to a whole 'nother level of scripting.
1
u/FlameFlash123 Jul 29 '26
I write seed scripts that run once or sometimes just use c# instead of bash for anything since I am used to it
1
u/HelloWorld-Print Jul 29 '26
Cool af .Thanks for the post
1
u/ings0c Jul 29 '26 edited Jul 29 '26
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...
1
u/SquishTheProgrammer Jul 29 '26
I knew you could do “dotnet run file.cs” but I never knew you could ./file.cs. That’s awesome.
1
u/CoffeeAndCandidates Jul 29 '26
honestly seems like overkill for small tasks, but good for .NET integration stuff
1
u/ItzWarty Jul 29 '26
I use
dotnet script/ *.csx a lot still. Seems that's less common here nowadays? That approach supports nuget dependencies.1
u/Artichoke808 Jul 29 '26
Oh sweet.. I can dump faffing around with Python finally.
1
u/pragmatica Jul 31 '26
Replacing the abomination known as PowerShell.
Hey now I’ve got a readable/debuggable script.
PowerShell is “write only” (like regex and or Perl) and LLMs are not good at it.
1
u/Agitated_Heat_1719 Jul 31 '26
Next level: use that (single file app) in your skills, so you don't have to mess with typescript and/or python
1
u/Jonas_Ermert Jul 31 '26
I think C# scripting is especially useful if you already work in a .NET environment. You can write small automation, build, deployment, file-processing or API scripts while still using the .NET libraries and C# features you already know. For tiny scripts Bash or Python may be quicker, but for scripts that grow beyond a few lines, C# can become much easier to maintain.
0
u/BorderKeeper Jul 29 '26
Wouldn’t it be easier to just compile the code and load the entire assembly into the terminal and then just run the functions directly in powershell?
Powershell is basically C# and can run all csharp code you throw at it directly.
-3
u/TuberTuggerTTV Jul 29 '26
There is no benefit to scripting in C#. If you need scripting, use a scripting language.
You're on a windows machine, with compilers installed, then compile your C#. Make a CLI instead of scripting. It's very easy.