Header add

Run C# without a project file in .NET 10 file-based apps

In this article we will learn about how to run a C# program from a single .cs file, without creating a project, a .csproj file or a solution. This feature is called file-based apps and it comes with .NET 10. If you have ever wanted to test a small piece of C# code quickly, or write a small script in C# like you do in Python or PowerShell, this is for you.

The old way vs the new way

Until now, even a two-line "Hello World" needed a full project. You ran dotnet new console, and you got a folder with a .csproj file, Program.cs, and later bin and obj folders. With .NET 10, you only need one file.

Classic console project vs .NET 10 file-based app

Behind the scenes, the .NET SDK creates a temporary project for you and builds it, so you never see it.

Prerequisites

You only need the .NET 10 SDK or later. Open a terminal and check your version:

dotnet --version

If it shows 10.0 or higher, you are ready. Any editor works, even Notepad.

Step 1: Your first file-based app

Create a new folder, add a file named hello.cs and write just one line:

Console.WriteLine("Hello from a single C# file!");

Now run it:

dotnet run hello.cs

There is an even shorter way. You can skip the word run:

dotnet hello.cs

The first run takes a few seconds because the SDK builds the app. The next runs are faster.

Step 2: Pass arguments

Create greet.cs. The args variable is available automatically, just like in a normal console app:

var name = args.Length > 0 ? args[0] : "friend";
Console.WriteLine($"Hello, {name}! Today is {DateTime.Now.DayOfWeek}.");

Run it and pass your name after --:

dotnet run greet.cs -- JT

Tip: With dotnet run, arguments after -- go to your app. With the short form dotnet greet.cs JT, all arguments go to your app, so you don't need --.

Running a .NET 10 file-based app in the terminal

Step 3: Use a NuGet package with #:package

There is no .csproj, so where do NuGet packages go? You add them at the top of the file with a special #:package directive. Let's use the popular Humanizer package. Create age.cs:

#:package Humanizer@2.14.1

using Humanizer;

var dotnet10Release = new DateTime(2025, 11, 11);
var age = DateTime.Now - dotnet10Release;

Console.WriteLine($".NET 10 was released {age.Humanize()} ago.");

Run dotnet age.cs. The SDK restores the package automatically and prints something like ".NET 10 was released 54 weeks ago."

Note: Always add the version after @. You can use @* for the latest version. Leaving out the version only works if you use central package management (a Directory.Packages.props file).

Step 4: Build a Web API in one file

File-based apps are not only for console code. Use the #:sdk directive to switch to the Web SDK, and you can run a small Minimal API from one file. Create api.cs:

#:sdk Microsoft.NET.Sdk.Web

var app = WebApplication.CreateBuilder(args).Build();

app.MapGet("/", () => "Hello from a single-file API!");
app.MapGet("/time", () => new { Now = DateTime.Now });

app.Run();

Run dotnet api.cs and open the URL shown in the terminal in your browser. That's a working API with zero project files!

All the directives at a glance

Directives start with #: and must be placed at the very top of the file.

File-based app directives #:sdk #:package #:property #:project
  • #:sdk chooses the SDK. The default is Microsoft.NET.Sdk (console).
  • #:package adds a NuGet package.
  • #:property sets any MSBuild property, for example #:property TargetFramework=net10.0.
  • #:project references another project, for example a shared class library.

Step 5: Publish your file

You can publish a file-based app just like a project:

dotnet publish hello.cs

File-based apps use Native AOT publishing by default, so you get a fast, self-contained executable. If a package you use doesn't support AOT, turn it off with #:property PublishAot=false at the top of the file.

Step 6: Convert to a full project when it grows

Your small script may become a real app one day. When that happens, convert it to a normal project with one command:

dotnet project convert hello.cs

This creates a folder named hello with your .cs file and a new hello.csproj. All your #:package, #:sdk and #:property directives are moved into the .csproj file automatically. Use --output MyProject if you want a different folder name.

When should you use file-based apps?

Good fitBetter as a normal project
Quick tests and learning C#Apps with many files and folders
Small automation scripts (rename files, call an API, clean data)Apps with unit test projects
Trying a NuGet package before using itLarge team projects in Visual Studio
Small demo APIsProduction apps that need full project settings

Conclusion

In this article we learned how to run C# without a project file using .NET 10 file-based apps. We ran a one-line program, passed arguments, used a NuGet package with #:package, built a small Web API with #:sdk, published it and finally converted it to a normal project. Next time you want to test a quick idea in C#, skip dotnet new and just create a .cs file.

Have you tried file-based apps yet? Let me know in the comments below.

Note: The images in this post are illustrations to explain each step.

Post a Comment

Previous Post Next Post