Skip to sessions and notes

These session notes are AI-generated and may contain mistakes. Verify important details against the original recording.

C# File-based apps turned up to 11!

Damian Edwards

Session · 27:09 recording · 7 min read

At a glance

  • 100% live code exploration of C# File-Based Apps (FBAs) in .NET 11
  • Full-fat C# & virtual project synthesis replacing CSX scripting
  • ~100ms cached startup bypassing MSBuild & Roslyn evaluation
  • Rich directives: #:package, #:property, #:include, #:ref, #:sdk
  • Central Package Management (CPM) omitting versions via Directory.Packages.props

Executive Summary

In this 100% live-code session, Damian Edwards unveils the evolution of C# File-Based Apps (FBAs) in .NET 11. First introduced in .NET 10, file-based apps allow developers to run, compile, publish, and package standalone .cs files without creating an explicit .csproj project file or a solution directory.

In .NET 11, File-Based Apps evolve from simple single-file scripts into an enterprise-grade execution model. Damian demonstrates that FBAs are full-fat C#, completely distinct from legacy CSX C# scripting: they utilize standard Roslyn compilation, support full C# Dev Kit language server IntelliSense, and leverage intelligent virtual project caching. When files are unchanged, the launch cache executes in ~100 ms, completely bypassing MSBuild and Roslyn.

Key innovations introduced in .NET 11 include:

  1. Rich Directive Syntax & Metadata: #:package with version floating or Central Package Management (CPM), #:property, #:include with globbing, and metadata tags like PrivateAssets=all.
  2. App-to-App & Project References: Linking multiple file-based apps via #:ref other.cs and traditional MSBuild class libraries via #:project path/to/lib.csproj.
  3. Single-File ASP.NET Core & Host Integration: Web SDK support (#:sdk Microsoft.NET.Sdk.Web), convention-based settings (web.settings.json), and run configurations (web.run.json) with VS Code file nesting.
  4. Third-Party MSBuild SDKs: Referencing NuGet-distributed SDKs such as Cake (cake.cs) directly in single files.
  5. Toolchain-Wide Parity: dotnet format, dotnet package list --outdated --vulnerable, and community tools like dotnet-outdated operating directly on .cs files.
  6. Unix/Linux Native Shebang & Extensionless Execution: Native execution via #!/usr/bin/env -S dotnet --, chmod +x, and extensionless CLI tool distribution.

Architectural Deep Dive & Compilation Pipeline

File-Based Apps synthesize a virtual project in memory and cache artifacts within .dotnet/launch-cache. Depending on whether directives, code, or nothing has changed, the CLI selectively bypasses MSBuild evaluation and Roslyn compilation.

flowchart TD
    subgraph Input ["Invocation Channels"]
        CLI["dotnet run app.cs / dotnet app.cs"]
        STDIN["echo 'code' | dotnet run -"]
        NIX["./app (Shebang + chmod +x)"]
    end

    subgraph Parser ["Directives Parser & Hash Evaluation"]
        Parse["Scan Top-of-File Directives (#:package, #:property, #:include, #:ref, #:sdk)"]
        HashCalc["Compute Cache Key (Source Hash + Directive Fingerprint + CPM Context)"]
    end

    subgraph CacheBranch ["Cache Decision Engine"]
        CacheCheck{"Fingerprint Match in .dotnet/launch-cache?"}
        Hit["Skip MSBuild & Roslyn<br/><b>Execution Time: ~100 ms</b>"]
        MissCode["Directives Unchanged, Code Changed?<br/><b>Fast Recompile: ~400 ms</b>"]
        MissFull["Directives Changed / Uncached?<br/><b>Full MSBuild + Restore: ~1000 ms</b>"]
    end

    subgraph VirtualProj ["Virtual MSBuild Project Synthesis"]
        Synth[".csproj Virtual Memory Model Synthesized"]
        Restore["Implicit NuGet Restore (Transitive Dependencies)"]
        Emit["Roslyn Emit to Cached Assembly Output"]
    end

    subgraph Execution ["CoreCLR Execution"]
        RunCore["Execute Target Assembly with Arguments"]
    end

    CLI --> Parse
    STDIN --> Parse
    NIX --> Parse

    Parse --> HashCalc --> CacheCheck
    CacheCheck -- "Identical Cache Key" --> Hit --> RunCore
    CacheCheck -- "Source Delta Only" --> MissCode --> Emit --> RunCore
    CacheCheck -- "Directives / First Run" --> MissFull --> Synth --> Restore --> Emit --> RunCore

Key Technical Innovations

1. Ergonomics & Execution Models

Running standalone C# files requires zero scaffolding:

# Minimal invocation
dotnet hello.cs

# Explicit CLI command
dotnet run hello.cs

# Standard argument forwarding
dotnet run --file hello.cs -- --name "Damian" --verbose

# Unix STDIN piping
"Console.WriteLine(DateTime.UtcNow);" | dotnet run -

Session Introduction
Session Introduction

Damian Edwards kicks off the live-coding session covering single-file C# in .NET 11.

Running dotnet hello.cs
Running dotnet hello.cs

Executing dotnet run hello.cs directly in the terminal without a .csproj or solution file.

Piping Code via STDIN
Piping Code via STDIN

Executing dynamic C# piped directly from standard input using dotnet run -.


2. Not Scripting: Full-Fat C# & Language Server Parity

Unlike legacy C# Interactive (.csx) or script engines:

  • Full Roslyn compilation: Full support for C# 12, C# 13, and preview features (#:property LangVersion=preview).
  • Standard tooling: Visual Studio Code and Visual Studio C# Dev Kit offer complete code completion, semantic colorization, refactoring, code fixes, and hover documentation.
  • Top-Level Statements & Methods: Seamlessly combine top-level statements with local functions, records, and class declarations.

3. Directives System in .NET 11

Directives appear at the top of the .cs file using the #:directive [value] prefix:

#:property LangVersion=preview
#:property TargetFramework=net11.0
#:package Humanizer.Core@2.14.1
#:package Spectre.Console
#:include helpers/formatting.cs
#:include helpers/*.cs

Central Package Management (CPM) Support

If a Directory.Packages.props exists anywhere in the directory hierarchy, #:package PackageName can omit the version (@version). The CLI automatically binds the version declared in the CPM file, ensuring version consistency across both full projects and standalone files.

Package Directives
Package Directives

Referencing external NuGet packages using #:package Humanizer.Core@2.14.1.

Central Package Management
Central Package Management

Omitting version tags in #:package to inherit versions managed by Directory.Packages.props.

Directive Metadata (PrivateAssets=all)

In .NET 11, directives now accept MSBuild metadata attributes. This is crucial for analyzers, generators, and packages that must not flow transitively:

#:package StyleCop.Analyzers@1.2.0-beta.556 PrivateAssets=all

Directive Metadata
Directive Metadata

Adding PrivateAssets=all metadata to package references inside file-based apps.


4. Multi-File Apps & Modular Includes

Single-file apps often outgrow a single file. .NET 11 introduces #:include:

  • Explicit files: #:include helpers/formatting.cs pulls helper methods into the compilation.
  • Transitive dependency resolution: If formatting.cs declares #:package Humanizer.Core, that package is automatically pulled into the parent application.
  • Globbing: #:include helpers/*.cs includes all matching files. Note: Wildcards currently bypass the launch cache check, causing a full recompilation check.

Include Directive
Include Directive

Including modular helper files and experiencing IntelliSense file path completions.


5. Launch Performance Benchmarks (MeasureLaunch.ps1)

Damian benchmarks execution time across cache states:

Execution State Mechanism Duration
Cold / Uncached (--no-cache) Full MSBuild project generation, NuGet restore, Roslyn emit ~1,000 ms
Directive Delta MSBuild virtual project modified, dependencies re-resolved ~1,000 ms
Source Code Edit Only Skips MSBuild project synthesis; fast direct Roslyn compilation ~400 ms
Warm Cache Hit (Unchanged) Bypasses MSBuild & Roslyn entirely; runs cached binary directly ~100 ms

Launch Timing Benchmarks
Launch Timing Benchmarks

Live execution timing: uncached (1.0s), code changed (400ms), cached (~100ms).


6. Single-File ASP.NET Core & Host Integration

Building complete web services in a single file:

#:sdk Microsoft.NET.Sdk.Web

var app = WebApplication.Create(args);
app.MapGet("/", () => Results.Ok(new { status = "online", timestamp = DateTimeOffset.UtcNow }));
app.Run();

Convention-Based Configuration & Run Settings

  • Settings: web.settings.json is automatically loaded by WebApplication.Create() or Host.CreateApplicationBuilder() (matching appsettings.json behavior).
  • Run Profiles: web.run.json replaces Properties/launchSettings.json, configuring ports, environment variables, and ASPNETCORE_ENVIRONMENT.
  • VS Code Nesting: With file nesting enabled, web.settings.json and web.run.json automatically nest cleanly beneath web.cs.

File-Based Web App
File-Based Web App

Running a full ASP.NET Core web server directly from dotnet web.cs.

File Nesting and Launch Settings
File Nesting and Launch Settings

VS Code Explorer displaying web.run.json and web.settings.json nested under web.cs.


7. Polyglot Ecosystem & Reference Topologies

.NET 11 bridges file-based apps with the broader .NET ecosystem:

  • Third-Party SDKs (#:sdk Cake.Sdk): Enables single-file C# build scripts with Cake types and targets imported out-of-the-box.
  • App-to-App References (#:ref plan.cs): Allows one file-based app to consume types and classes from another .cs file without packaging it to NuGet.
  • Traditional Project References (#:project ../projects/brand.csproj): Allows a lightweight file-based app to link directly to enterprise class libraries and domain models.

SDK Directive and Cake
SDK Directive and Cake

Using #:sdk Cake.Sdk to write a single-file C# build script.

App References and Project References
App References and Project References

Combining #:ref plan.cs and #:project ../projects/brand.csproj in a single file.


8. CLI Toolchain Parity & Package Hygiene

In .NET 11, the entire .NET CLI suite has been updated to treat .cs files as first-class project targets:

  • dotnet format formatme.cs: Formats code against editorconfig conventions.
  • dotnet package list --file packages.cs: Inspects top-level and transitive dependencies.
  • dotnet package list --file packages.cs --outdated: Flags packages with newer versions on NuGet.
  • dotnet package list --file packages.cs --vulnerable: Scans for known security vulnerabilities.
  • dotnet outdated packages.cs: Community tool .NET Outdated updated with native file-based app flags (--enable-fb-apps -r).

dotnet format on Single File
dotnet format on Single File

Running dotnet format formatme.cs to apply whitespace and code style rules.

Package List Outdated and Vulnerable
Package List Outdated and Vulnerable

Listing outdated and vulnerable NuGet packages directly on a .cs file.


9. Linux/Unix Native Shebang & Extensionless Execution

To integrate into Unix workflows and CLI path tooling:

  1. Add the shebang to line 1 of the file:
    #!/usr/bin/env -S dotnet --
    using System;
    Console.WriteLine($"Running as native CLI command. Args: {string.Join(", ", args)}");
    
  2. Make the file executable:
    chmod +x session.cs
    ./session.cs arg1 arg2
    
  3. Extensionless Support: Create an extensionless file or symlink (session -> session.cs). The .NET CLI detects the shebang and executes the file seamlessly. Placing it in ~/.local/bin allows invoking session from any shell prompt.

Linux Shebang and Extensionless Execution
Linux Shebang and Extensionless Execution

Configuring Unix shebang, chmod +x, and extensionless execution in Ubuntu WSL.


Conclusion & Roadmap

File-Based Apps in .NET 11 elevate C# into a premier language for shell scripting, CLI tools, micro-benchmarks, rapid prototyping, and web utilities. By combining ~100ms cached startup, full Roslyn language features, rich directive metadata, and complete CLI command parity, developers get the agility of Python or Node.js with the performance, type safety, and ecosystem power of .NET.

Look for continued refinements in upcoming .NET SDK feature band releases (11.0.200 in early 2027), including further optimizations for glob caching and language server enhancements.