Modern C# · Lesson 45

.NET 10 file-based apps and build directives

Run an actual SDK-managed single-file app externally; compare its domain rule here.

External .NET 10 SDK / stable C# 14. This project does not run in SharpRune's .NET 9 playground.

Read or download the complete SDK project, then follow its setup on your computer. The separate C# companion practice below runs in SharpRune’s .NET 9 console environment.

The concept and environment

File-based apps are a .NET 10 SDK workflow for building and running a C# source file without writing a traditional project file. The external Program starts with real build directives and is executed with dotnet run followed by its explicit file option. The portable companion contains only the same bounded calculation, with no SDK directives. It therefore compares program logic, not the file-based app feature itself. SharpRune's current .NET 9 browser runner is not an installed .NET 10 command-line SDK and does not execute these external configuration directives.

Walk through the portable program

The shared calculation creates quantities two, three, and four, then adds them using checked integer arithmetic. There are three items and the total is nine, so the output is items:3 followed by total:9. No command-line input, network request, native library, or data file is needed by the calculation. Its determinism makes it easy to distinguish application output from SDK build diagnostics. The companion establishes those two expected domain observations while leaving the genuine source-file discovery, build configuration, compilation, and process launch to the separate external SDK command.

Compile the genuine external feature

The external file uses property directives to select net10.0, stable language version 14.0, and PublishAot false. Save it exactly as Program.cs in a fresh directory, then run dotnet run --file Program.cs using a suitable .NET 10 SDK. The SDK interprets the directives and creates the necessary build configuration; no FeatureLab project is supplied for this example. The explicit file option removes ambiguity with ordinary project execution. Compiling the companion without those directives cannot verify this tooling feature, even though it produces the same two application-output lines.

Pitfalls and limits

Build directives are not interchangeable with ordinary runtime statements or with arbitrary preprocessor syntax accepted by every compiler. Keep them at the top of the external source. The chosen PublishAot setting is explicit configuration; this lesson runs the managed app and does not claim to publish or validate a native executable. Current documentation may also describe later SDK additions, so do not assume every newly documented directive or command exists in every .NET 10 patch. This complete baseline deliberately uses only the actual property directives verified with its stated SDK.

Practice one: isolate a contract

First add a fifth quantity and predict the new item count and sum before running both programs. Hint: verify the calculation independently from whether the SDK successfully interprets the external file. Then change only the external target framework to an unavailable framework in a separate copy and observe a build failure rather than treating it as a domain-result mismatch. Restore the supported target afterward. Keep diagnostic output and expected application output distinct so a successful arithmetic prediction cannot disguise a failure to build the real external app.

Practice two: challenge an assumption

For another exercise, create a second independent file-based app in a clean directory that computes a small validated report, keeping its configuration explicit and its result deterministic. Hint: invoke the intended file with the file option rather than relying on the current directory's project discovery. If you later require packages or project references, consult the documented directive and SDK-version support before adding them. Do not paste package directives into the portable companion and claim that SharpRune installed an SDK restore workflow merely because the underlying arithmetic still runs.

Review the resulting contract

Review a file-based app by checking the SDK version, selected source path, directive support, target framework, and exact launch command before inspecting its domain results. The external sample demonstrates actual .NET 10 tooling with a complete single source file and no hidden project file in the downloadable content. The portable comparison is intentionally simpler and makes no tooling claim. Use file-based apps where their lightweight packaging fits the task, then move to a deliberate project structure when dependencies, build customization, or maintenance needs make that structure clearer to the people using it.

External .NET 10 SDK / stable C# 14. This project does not run in SharpRune's .NET 9 playground.

.NET 10 file-based apps and build directives lab

Set up the SDK project

  1. Save every listed file at its exact relative path in a fresh directory.
  2. Install a .NET 10 SDK supporting stable C# 14; check dotnet --version.
  3. Run dotnet run --file Program.cs in that directory. No .csproj is required; #: directives belong to the external .NET 10 SDK workflow.

Download complete SDK project ↗

Project files

Program.csDownload file ↗
#:property TargetFramework=net10.0
#:property LangVersion=14.0
#:property PublishAot=false
using System;
int[] quantities = [2, 3, 4];
int total = 0;
foreach (int quantity in quantities) total = checked(total + quantity);
Console.WriteLine($"items:{quantities.Length}");
Console.WriteLine($"total:{total}");

Expected result

Expected console output, in this order: items:3 total:9 The portable companion compares the same small rule using older syntax; it does not compile or emulate the new feature.

Use the listed SDK and dependencies for this project. Open C# practice runs the separate portable exercise.

C# companion practice

using System;
int[] quantities = [2, 3, 4];
int total = 0;
foreach (int quantity in quantities) total = checked(total + quantity);
Console.WriteLine($"items:{quantities.Length}");
Console.WriteLine($"total:{total}");
Open C# practice

C# practice output

items:3
total:9
Try this
The directives and dotnet run --file belong only to the external app. This companion runs its calculation without pretending to execute SDK configuration.

Further reading