loading experience

.NET

.NET 10: running a single C# file with dotnet run

Scripts, prototypes and internal tools without a project: file-based apps and #:package directives.

.NET 10: running a single C# file with dotnet run

To try an idea in C# you always needed a project: folder, .csproj file, structure. With .NET 10 a single file is enough: dotnet run app.cs compiles and runs it.

A single file grows into a full project without starting over.
A single file grows into a full project without starting over.

The first script

// hello.cs
Console.WriteLine($"Hello from .NET {Environment.Version}");
dotnet run hello.cs

Packages and settings in the file

Directives starting with #: replace the project file: you can add NuGet packages, change SDK or set build properties.

#:package Humanizer@2.14.1

using Humanizer;

var days = TimeSpan.FromDays(3);
Console.WriteLine(days.Humanize());

For a small web API you can declare the web SDK with #:sdk Microsoft.NET.Sdk.Web and write a Minimal API in the same file.

When the file grows

When the script becomes a real application, the dotnet project convert command turns it into a full project, keeping the packages and settings declared in the directives.

Practical uses

  • Maintenance and automation scripts instead of batch or PowerShell files.
  • Quick prototypes and library experiments.
  • Teaching examples: a single file to share.
Comments (0)

No comments yet.

Leave a comment