.NET 10 introduces the ability to run C# files directly without a csproj
file using dotnet run app.cs
.
Add NuGet package references using the #:package
directive.
#:package Humanizer@2.14.1
using Humanizer;
var dotNet9Released = DateTimeOffset.Parse("2024-12-03");
var since = DateTimeOffset.Now - dotNet9Released;
Console.WriteLine($"It has been {since.Humanize()} since .NET 9 was released.");
By default, file-based apps use the Microsoft.NET.Sdk
. To use a different SDK, use the #:sdk
directive.
#:sdk Microsoft.NET.Sdk.Web
Configure additional build properties with #:property
directives.
#:property LangVersion preview
#!/usr/bin/dotnet run
Console.WriteLine("Hello from a C# script!");
You can make the file executable and run it directly:
chmod +x app.cs
./app.cs
As of preview 4, it does not support a path like #!~/.dotnet/dotnet run
.
To convert to a project-based app,
dotnet project convert app.cs
This command creates a new directory named for your file, scaffolds a .csproj
file, moves your code into a Program.cs
file, and translates any #:
directives into MSBuild properties and references.
The specification for this feature is in the dotnet/sdk
repo at dotnet-run-file.md.