from Hacker News

Run a C# file directly using dotnet run app.cs

by soheilpro on 5/29/25, 2:30 AM with 326 comments

  • by jbverschoor on 5/29/25, 7:33 AM

    Great, but unfortunately, even when compiled, the startup overhead is about half a second, which makes it unsuitable for many applications. Still I applaud it, as Shell scripting is finicky, people thend to rely on bash features, and perl is kind of over. Ruby was, and still is, my go-to language for this purpose, but I've recently migrated some scripts over to Swift.

    Swift does a much better job at this as interprets by default, and a compiled version starts instantaneously. I made a transparent caching layer for your Swift cli apps.Result: instant native tools in one of the best languages out there.

    Swift Script Caching Compiler (https://github.com/jrz/tools)

    dotnet run doesn't need it, as it already caches the compiled version (you can disable with --no-build or inspect the binaries with --artifacts-path)

  • by pjmlp on 5/29/25, 7:59 AM

    I find it a pity the lack of acknowledgement of the CSX/VBX effort.

    https://ttu.github.io/dotnet-script/

    Or that they on the wisdom of the C# Language Runtime, decided on an incompatible approach to how F# references dependencies on its scripts.

    https://learn.microsoft.com/en-us/dotnet/fsharp/tools/fsharp...

  • by donatj on 5/29/25, 1:02 PM

    It's interesting that they're actively promoting using it with a shebang. I find it pretty appealing.

    Go prior to modules worked really well this way and I believe Ubuntu was using it like this, but the Go authors came out against using it as a scripting language like this.

  • by occz on 5/29/25, 7:02 AM

    This also exists for Kotlin: https://github.com/Kotlin/kotlin-script-examples/blob/master... (note that the file extension is load bearing in this case, if you don't name it "*.main.kts", it will not work).

    It's excellent for writing small scripts/prototyping where you need access to some Kotlin/JVM feature.

    Ruby is still my preferred language for small scripts though - the backticks for running external programs is remarkably ergonomic

  • by pragmatic on 5/29/25, 5:15 AM

    Seems like a great PowerShell replacement.

    PowerShell is the ultimate Chatgpt language. For better or worse. Usually worse as most shops end up with "write only" PowerShell scripts running all the glue/infrastructure stuff.

  • by wiso on 5/29/25, 8:00 AM

    You can also use shebang to run C# scripts like bash scripts https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-...
  • by moogly on 5/29/25, 7:29 AM

    Kind of obsoletes NetPad, and as soon as there's debugging, LINQPad can be put out to pasture. LINQPad was instrumental for me many years ago, and I appreciate that, but that stone-age text editor is not usable for actually writing/editing code in this decade.
  • by andix on 5/29/25, 6:55 PM

    This feature is probably a big thing for .NET developer productivity. It's quite a shame, that it only came now.

    There is another thing I'm really missing for .NET projects. The possibility to easily define project-specific commands. Something like "npm run <command>"

  • by mrcsharp on 5/29/25, 6:57 AM

    I'm excited for this one. I can see it replacing some of the powershell scripts I have in CI/CD pipelines.

    As much as I like Powershell and Bash, there are some tasks that my brain is wired to solve more efficiently with a C-like syntax language and this fill that gap for me.

  • by adzm on 5/29/25, 6:15 AM

    There is even more info in the proposal itself https://github.com/dotnet/sdk/blob/main/documentation/genera... specifically things about multiple files and more specifics of the implementation and capabilities (implicit project file etc)
  • by adzm on 5/29/25, 4:06 AM

    Finally! Perfect for making a quick utility to use in a script. There have been third party solutions but having to install something to do this was always another obstacle / headache
  • by rjbwork on 5/29/25, 4:31 AM

    Very cool. Will probably still stick with LINQPad for my every day experimentation needs but if i decide to write actual scripts for ~production use, this would be a better alternative to Powershell in most cases I think.
  • by throwaway896987 on 5/29/25, 7:29 AM

    The oldest one: https://github.com/oleg-shilo/cs-script

    Used it 10+ years in production, but usually no one I talk to in .net world has ever heard about it.

  • by WuxiFingerHold on 5/30/25, 2:35 AM

    >> Install Visual Studio Code (recommended) If you’re using Visual Studio Code, install the C# Dev Kit

    I wish Microsoft would just provide a LSP server for C#. Not just a half proprietary extension for VS Code.

  • by TowerTall on 5/29/25, 3:27 PM

    i couldn't help noticing that none of the autocomplete suggestions in his terminal or VS Code were relevant or correct. It looked really annoying. Is that the usual quality Copilot delivers?
  • by necovek on 5/29/25, 4:22 AM

    Any number of these small papercut-like things is what has made a tool like Python ubiquitous.
  • by resoluteteeth on 5/29/25, 1:11 PM

    I guess this continues the trend of c# eventually getting all the features from f#
  • by sirjaz on 5/29/25, 1:12 PM

    It seems Microsoft is trying to make C# more flexible and attractive to Data Scientists and beginners. They should have done this years ago.
  • by high_na_euv on 5/29/25, 11:06 AM

    Seems like a great Python replacement.
  • by jiggawatts on 5/29/25, 5:15 AM

    There's a rule that if something is easy to invent, a bunch of people will whip up their own versions independently, sometimes even within the same organisation.

    So for there's...

    C# scripting (CSI.exe): https://learn.microsoft.com/en-us/archive/msdn-magazine/2016...

    PowerShell Add-Type that allows one-file inline C# scripting: https://learn.microsoft.com/en-us/powershell/module/microsof...

    The Roslyn .CSX script files (RCSI.exe): https://devblogs.microsoft.com/visualstudio/introducing-the-...

    .NET Interactive: https://github.com/dotnet/interactive

    ... and now this.

  • by zeroq on 5/29/25, 12:18 PM

    Some 20y ago I used "scratchpad" or whatever it was called to write short "bash" scripts and run them directly from that "notepad"... and it was a blast.

    I never understood why it has to be so hard on Windows to enable users to do just a little bit of scripting like it's not 80's anymore.

  • by masfoobar on 5/30/25, 7:49 AM

    Its good to finally see this being 'a thing' in C#. In my opinion, it is 10 years overdue!

    This feature is likely added to compete with Python, Ruby, etc. The fact you just create a file, write some code and run it.

    However, I don't see C# being a competitor of said languages even for simple command lines. If anything, it could be a viable replacement to Powershell or maybe F# especially if you need to link it to other .NET DLLs and 'do things'

    I am also interested in the performance difference compared to other languages. I mean even Dlang has a script-like feature for sometime, now.. rdmd. Not sure the status of that, but it still compiled and runs the program. Just seems overkill for something rather simple.

  • by nayuki on 5/29/25, 3:33 PM

    Java SE 11 (Sep 2018) introduced the ability to run source files directly. So instead of the old `javac Main.java && java Main`, you can do `java Main.java`. https://openjdk.org/projects/jdk/11/ , https://openjdk.org/jeps/330
  • by theanonymousone on 5/29/25, 1:06 PM

    As nobody mentioned before, Java has had JBang for a while now: https://www.jbang.dev/
  • by DMiradakis on 5/29/25, 9:42 PM

    I'm building a dotnet job orchestrator called Didact (https://www.didact.dev), and this is the sort of thing I was looking for years ago when I was first dreaming it up. Class libraries is the approach I am taking now, but this is still extremely interesting. Could see some interesting use cases for this...
  • by pipeline_peak on 5/30/25, 2:49 AM

    On the topic of scripts, it would be nice if they had a more seamless way to invoke processes. The whole Process.Start() way can be convoluted when I just want something simple.

    Either way, this is a dream come true. I always hated Powershell and it’s strange fixation with OOP. Modern C# seems to acknowledge that sometimes all we want are functions, scripts, Linq and records.

  • by ComputerGuru on 5/29/25, 2:09 PM

    C# is my second-favorite language. I made a shebang to do the same for rust (my number one) many years ago with zero (external) dependencies: https://neosmart.net/blog/self-compiling-rust-code/
  • by tgma on 5/29/25, 6:15 AM

    Didn't Mono implement something like this ages ago? And I mean ages ago, before C# even had standardized code outside "static void Main" in the language spec, IIRC they had an interpreter that probably used Reflection.Emit or something and executed the output and you could #! it.
  • by 90s_dev on 5/29/25, 2:48 PM

    With Java allowing void main() {} like C,

    and now shebang C# scripts,

    is everything converging into one meta language?

  • by xunil2ycom on 5/29/25, 2:25 PM

    I've been coding in C# for more than a decade and a half now, and I see myself using this rather than creating an entire project structure to test some new approach to something.
  • by replwoacause on 5/30/25, 1:56 AM

    As someone just getting into C# from PowerShell, this is huge.
  • by GoblinSlayer on 5/29/25, 7:01 AM

    >Because of the implicit project file, other files in the target directory or its subdirectories are included in the compilation.

    Errm, so how is this different from a folder with a project file?

  • by zerr on 5/29/25, 7:50 PM

    How is it going with native AOT? I'd rather prefer:

    dotnetc app.cs

    ./app

  • by CommonGuy on 5/29/25, 4:24 AM

    this will make scripting with C# so much easier
  • by DeafMan1983 on 5/30/25, 9:41 PM

    What is about NativeAot of Dotnet?

    If you run direct Dotnet App?

  • by naikrovek on 5/29/25, 12:57 PM

    I can’t believe it took this long.

    It is insane to me how long it takes people to realize that low barriers for execution and experimentation are important.

    Imagine if the Commodore 64 or Microsoft Basic required you to scaffold your program with helper files containing very specific and correct information in order to run those programs.

  • by arrty88 on 5/29/25, 10:33 PM

    Would be cute to alias dnx to get ‘dnx app.cs’
  • by ThinkBeat on 5/29/25, 1:15 PM

    Announcing proudly that botnet can now compile without a csproj file seems a bit over excited.

    It his basically:

    gcc test.c -o test.exe test.exe.

    and

    #!/ comple and run gcc test.c -o test.exe test.exe.

  • by revskill on 5/29/25, 5:29 AM

    .csproject and visual studio is the main reason i avoid csharp. The whole setup feels dumb for no reason.
  • by DiabloD3 on 5/29/25, 7:06 PM

    This isn't a new feature, and was announced for... I think 8.

    Why is it being re-announced now?

  • by renatovico on 5/29/25, 4:12 PM

    very cool, but only one thing come in mind, the new .VBS !
  • by mhh__ on 5/29/25, 5:34 AM

    Maybe one day they'll bite the bullet and let you write a function without extra 10 keywords and a sprinkling of nouns

    I really want to like C# but there is a reason why it has no ecosystem outside of enterprise and gaming slop

  • by thom on 5/29/25, 10:45 AM

    I know C# people like to think up three new forms of syntax before breakfast, but it feels like these magic comments could have been attributes?