File-based .NET apps: one .cs file and a #:package directive

  • csharp
  • dotnet

For years the smallest thing you could build with the .NET SDK was a folder with a .csproj, a Program.cs, and two bin/obj directories you immediately added to .gitignore. Most of my throwaway tools never got written because the ceremony outweighed the value. .NET 10 removes that friction: a single .cs file is now a runnable app.

The minimum

// hello.cs
Console.WriteLine($"Running on {Environment.OSVersion}");
dotnet run hello.cs

Behind the scenes the SDK generates a transient project for the file, restores, compiles and runs it. The generated project uses Microsoft.NET.Sdk, targets net10.0, and enables nullable reference types and implicit usings, so System, System.IO, System.Linq and friends are already available without a single using line.

Referencing packages: #:package

The part I underestimated is the #:package directive. Lines that start with #: at the top of the file are read before compilation and feed the generated project:

#:package CsvHelper@33.0.1
#:property TargetFramework=net10.0
#:property InvariantGlobalization=true

using System.Globalization;
using CsvHelper;

var totals = ReadSales("sales.csv")
    .GroupBy(s => s.Region)
    .Select(g => new { Region = g.Key, Total = g.Sum(s => s.Amount) })
    .OrderByDescending(t => t.Total);

foreach (var t in totals)
    Console.WriteLine($"{t.Region,-12}{t.Total,14:N2}");

static IEnumerable<Sale> ReadSales(string path)
{
    using var reader = new StreamReader(path);
    using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
    foreach (var sale in csv.GetRecords<Sale>())
        yield return sale;
}

record Sale(string Region, decimal Amount);

#:property sets MSBuild properties on the generated project, so anything a .csproj can do works here too: PublishAot=true, InvariantGlobalization=true, LangVersion=preview. If you would rather not type the directive by hand, dotnet add package CsvHelper --file-based sales.cs writes it for you.

The SDK itself can be swapped too. #:sdk Microsoft.NET.Sdk.Web as the very first line is enough to host a minimal API from a single file, which is a nice way to mock a third-party service for an afternoon:

#:sdk Microsoft.NET.Sdk.Web

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/time", () => DateTime.UtcNow);
app.Run();

A few details that tripped me up:

  • The directives must come before any using or namespace. The parser is strict about position.
  • Restore results are cached per file, so a second run starts in well under a second as long as the directives have not changed.
  • dotnet run does not mean “interpreted”. The file is compiled with the same compiler as any project; you get the same warnings and the same Nullable diagnostics.

Where it stops being enough

I still move to a real project when any of these show up:

  • more than one file, or a type that clearly belongs in a shared library;
  • tests, because a test project needs a project reference, not a loose script;
  • anything that needs Directory.Build.props, a source generator configured in the .csproj, or a custom SDK.

The upgrade path is deliberately short. dotnet project sales.cs turns the file into a regular console project with the directives translated into a .csproj, so nothing written for the file-based version is throwaway.

What I use it for

Daily build scripts, log munchers, one-off data migrations against a CSV dump, benchmarks of a single method. Anything that fits on one screen and has no tests belongs in a file, not in a folder with forty megabytes of obj underneath it.