Hi lovely readers,
Sometimes you just need a quick script in the language you are most comfortable with, C# for example, but then you have to go through the hassle of creating a solution with a project and a bunch of extra files you probably do not need, just to run ten lines of code. Using C# for a small task has always felt like overkill.
.NET 10 now fixes this with file-based apps. It introduces a new lightweight execution model that lets you run a C# program contained in a single file with no project or solution setup at all.
In this blog post, we are going to take a look at file based apps and how to use them.
Why file-based apps
-
No project overhead
Write your C# code in one
.csfile and run it directly. That is all you need. There is no.csprojfile, no solution, and no boilerplate. This simplicity means you can get started right away when you want to build a quick tool or demo. -
Great for prototyping and scripting
File-based apps are perfect for prototypes, one off console utilities, CI or CD scripts, or quick data processing tasks. Instead of creating a full project for a ten line script, you can just run a single file. It makes C# feel competitive with traditional scripting languages like PowerShell or Bash.
-
Full power of C# and .NET
You are still writing a real C# program. You get the strong type system, performance, LINQ, the full .NET libraries, and even NuGet packages. Nothing is limited just because you are using one file.
-
Full-on CLI support
This feature is built right into the .NET CLI. No extra tools or interpreters are required. Just run
dotnet run <YourFile>.csand the CLI takes care of the rest. On Unix like systems, you can even add a shebang so you can run your file like a regular script. -
Easy to scale up
A file-based app is not something you will outgrow. If your single file script becomes something larger, the .NET 10 SDK includes a tool that converts it into a full project with a
.csproj, keeping your package references and settings.
How to use file-based apps
Create and run a single file program
To get started, all you need is the .NET 10 SDK installed. You can install it from dotnet.microsoft.com. In whatever text editor or IDE you want write your code in a single C# file. You can use top-level statements that were introduced in the past so you don’t need a Main method.
For example, create a file called DiceThrow.cs:
// DiceThrow.cs
var rng = new Random();
int roll = rng.Next(1, 7);
Console.WriteLine($"You rolled a {roll}!");
Then open your terminal in the same folder and run:
dotnet run DiceThrow.cs
The CLI creates a temporary project in the cache, compiles your file, and runs it. The first run takes a moment, but after that it launches almost instantly.
Using NuGet packages with #:package
Sometimes you will need some extra help from a NuGet package. You can add those to your script too. If your script needs a NuGet package, you can add it at the top of your file.
For example, here is a script that uses Figgle to create ASCII art. Save it as ASCIIArt.cs:
#:package Figgle@0.6.5
#:package Figgle.Fonts@0.6.5
using Figgle;
using Figgle.Fonts;
Console.WriteLine(FiggleFonts.Standard.Render("Hello World"));
The version numbers after the at sign are required. If you are not sure which version to use, the NuGet Gallery page shows a ready to copy snippet under the File-Based Apps tab. For example, the Figgle NuGet Gallery page looks like this:
Running your C# file as a script on Unix
If you like the convenience of running scripts directly, you can add a shebang to the top of your file.
#!/usr/bin/env dotnet run
Console.WriteLine("Hello from shebang!");
Make it executable:
chmod +x Shebang.cs
And finally run it like this:
./Shebang.cs
It feels just like running any other script.
To keep in mind
- Only one file is supported right now. Multi file support is coming in .NET 11.
- These are compiled C# programs, not interpreted scripts.
- You can debug your script in your IDE by opening the file and setting breakpoints.
That is a wrap
File-based apps in .NET 10 make C# feel more flexible and accessible for small tasks. You no longer have to switch to Python or Bash when you want to automate something quickly. Now you can stay in the language you love, with your favorite NuGet packages available right away.
I hope this post helped you understand file-based apps in modern C#. If you have any questions or comments, feel free to reach out on almost every social media platform (louella.dev/socials) or leave a comment down below.
See ya!


Top comments (0)