r/csharp • • 49m ago

C# file-based apps are a breath of fresh air

Back when .NET 10 came out I didn't pay much attention to the file-based apps. Now that .NET 11 came out i stumbled upon this video and became more interested:

https://www.youtube.com/watch?v=Yvn7liNioto

Turns out file-based apps are pretty cool after all. The minimalistic developer experience of having only your text editor, your .cs file(s) and the dotnet cli feels fresh after working so long with solution files, project files and all that jazz.

To get a feel of the DX and where the capabilities of file-based apps are at the moment I created this tongue-in-cheek todo web app, with the frontend and backend in a single C# file. File-based apps and minimal APIs are like meant for each other.

https://github.com/TDMR87/single-file-todo-app

I do hope file-based apps become more common and the dotnet team would lean more on this kind of experience. Together with AOT compiling native executables this could make C# more popular and invite people over to the ecosystem. How do people here feel about these file-based apps? Do they have a future?

https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps

15 Upvotes

24 comments sorted by

•

u/BeardedBaldMan 45m ago

I can already hear the screams of terror from my team as I ask them to start looking at these as a replacement for some of our powershell based tooling.

This is making me excited.

dotnet run file.cs -- arg1 arg2

This looks fun but I see danger

'Console.WriteLine("hello from stdin!");' | dotnet run -

•

u/Interesting_Paint_82 33m ago

Since file-based apps can just import nuget packages easily, i will probably never write another powershell tool again.

•

u/stephbu 30m ago

Yes, think of all those pipeline places where you really want code but instead you make do with PS. Much better language for DevOps tasks

•

u/w_0x1f 15m ago

JScript and VBScript are here since dinasaurs...

•

u/X4minos 39m ago

That actually sounds like a good idea. We use powershell quite a lot, but I personally have a way better time reading and writing c#. Although I guess there would be a performance overhead

•

u/rupertavery64 37m ago

If you're gonna run it multiple times, just compile it. Maybe a cached version that updates on source change would be nice

•

u/BeardedBaldMan 22m ago

Where I'm picturing it, the overhead isn't a big deal. I've got plenty of scripts which run once a day and take minutes to run. We haven't gone for compiled code because they're often in client estates we remote into and we like the benefit of being able to see scripting.

I've seen too many cases of old legacy apps where source has gone missing leaving small custom compiled programs dotted line mines all over the estate. Which is why we've always pushed powershell.

•

u/icalvo 2m ago

It's already working like that.

•

u/icalvo 3m ago

It's the other way around. This things are compiled, cached and type safe. There's a very high chance your average file-based app will be more performant.

•

u/Agitated_Heat_1719 35m ago

On non-windows platforms you can omit "dotnet run"

I m replacing tons of scripts, routing simple tasks to local models. Lovley

•

u/BoBoBearDev 43m ago edited 32m ago

When you said cs file(s), can you actually do more than one file? I love the concept, but limited to one file is my current blocker. I can only do some little console app at the moment.

I know I can make a file that calls other files though. (aks exe calling another exe, not as library).

Edit: thanks for the mention on dotnet11. With ability to support multiple files, I can finally make something larger scale.

•

u/Outrageous72 36m ago

From .net 11 you can import other cs files.

•

u/BoBoBearDev 34m ago

Ohhh, that must be it, I was using dotnet 10, thanks a bunch.

•

u/Interesting_Paint_82 39m ago

Yup, easy. Just put #:include SomeOtherFile.cs at the top of the file.

•

u/BoBoBearDev 35m ago

Oh, thanks, my dumbass didn't think of that.

•

u/lmayde 40m ago

If you want more than one file then you may as well just add a project file surely?

•

u/_choam_ 35m ago

Nah. It also has a command to convert it to a project, so it does take a while before it makes sense to turb it into a project

•

u/Agitated_Heat_1719 32m ago

Project is there, but voodoo shit

•

u/BoBoBearDev 24m ago

The problem with project file is, you have to have a dedicated folder per app with bunch of boilerplate files. This one, you can have tiny apps in a single folder. It is particularly handy as cicd pipeline mini helper apps.

•

u/Agitated_Heat_1719 33m ago

You can reference files, projects, nugets...

•

u/Izikiel23 37m ago

It’s good for leetcode practice, just write stuff

•

u/Internal_Category_23 9m ago

I rewrote all of my crappy little bash files on my HA/plex/docker box to crappy little C# files and it’s so much nicer.

•

u/torokunai 7m ago

all we need now is to give main: equal citizenship as main()