SharpRuneOpen playground

.NET 11 file-based apps split across files

Keep a single-command app while #:include moves shared code into a second file.

  • 15 min
  • Read, predict, then run
External .NET 11 SDK / stable C# 15. 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.

At a glance

One command, two source files

  1. #!/usr/bin/env dotneta multi-file entry point without it gets CA2266
  2. #:property directivesTargetFramework=net11.0, LangVersion=15.0, PublishAot=false
  3. #:include Pricing.cswithout it the SDK compiles Program.cs alone (CS0103)
  4. dotnet run --file Program.csa .NET 11 SDK command; no .csproj file is needed
  5. Prints items:3 and total:45(2 + 3 + 4) × 5 with checked arithmetic
  6. SharpRune sample: same rule, one filethe .NET 9 playground runs only the calculation

The .NET 11 SDK reads the directives at the top of Program.cs, compiles the included file into the same app and runs it. Matching output in the playground checks the pricing rule, not the SDK workflow.

The concept and environment

.NET 10 introduced file-based apps: one C# file run by dotnet run with no project file. The .NET 11 SDK adds the #:include directive, which pulls another source file into the same build, so helpers can move out of the entry point without creating a project. The external app has a Program.cs with build directives and an include of Pricing.cs. SharpRune's .NET 9 companion holds the same pricing rule in one file. It compares the calculation; it does not execute SDK directives or emulate the include. Read the environment labels before copying either version.

Walk through the portable program

The companion multiplies each quantity by a unit price of five inside checked arithmetic and adds the results. Quantities two, three and four give ten, fifteen and twenty, for a total of forty-five. Output is items:3 and total:45. Pricing is a static class in the same file, so nothing needs to be found elsewhere. That single-file arrangement is what the external app splits apart: the class moves to Pricing.cs, and the entry point keeps only the input values and the two output lines. Neither file changes the rule itself.

Compile the genuine external feature

The external Program.cs starts with #!/usr/bin/env dotnet, then pins net11.0 and language version 15.0 with #:property directives and adds #:include Pricing.cs. Run it with dotnet run --file Program.cs; no project file is supplied. Without the include, the SDK compiles only Program.cs and reports CS0103 for Pricing even though the file sits beside it. The shebang line is not decoration: in a multi-file app the analyzers report CA2266 when the entry point lacks it. Native publishing is not performed or claimed here.

Pitfalls and limits

Directives must come before the first C# token of the entry file; a later one is rejected with CS9297. The included file is ordinary C# compiled into the same assembly, so duplicate type names collide as they would in a project. The .NET 11 SDK also rejects a colon inside a #:property name. The app in lesson 45 pins net10.0, so with only the .NET 11 SDK installed it builds but cannot launch until a .NET 10 runtime is present. The SDK version and the runtime an app needs are separate decisions.

Practice one: isolate a contract

First, add a discount rule to Pricing.cs in the external app and to the companion's Pricing class: an order of ten or more items gets one unit free. Predict the totals for the current input and for quantities four, three and three. Hint: write the expected totals from the rule before running either program, and keep the threshold boundary visible. Then remove the #:include line and read the compiler error, so you recognize a missing include instead of assuming a sibling file is compiled automatically.

Practice two: challenge an assumption

For another exercise, pass the unit price through an environment variable with the .NET 11 option dotnet run --file Program.cs -e PRICE=5, and read it with Environment.GetEnvironmentVariable in the external app only. Decide what happens when the variable is missing or is not a number. Hint: keep the parsing rule in Pricing.cs and parse with the invariant culture. The companion should keep its constant, because SharpRune's playground does not supply process environment variables or command-line SDK options. The variable is an input like any other and needs the same validation.

Review the resulting contract

Review a file-based app by reading its directives as the project file they replace: target framework, language version, included files and package references. #:include lets a small app grow past one file without a project file, but once several entry points, test projects or build customizations appear, converting to an ordinary project is still the clearer step. The external app exercises the real .NET 11 SDK workflow, while the portable companion keeps the pricing rule inspectable inside SharpRune. Neither version should be presented as the other, and the external result belongs to the stated SDK.

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

.NET 11 file-based apps split across files lab

Set up the SDK project

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

Download complete SDK project ↗

Project files

Program.csDownload file ↗
Program.cs
#!/usr/bin/env dotnet
#:property TargetFramework=net11.0
#:property LangVersion=15.0
#:property PublishAot=false
#:include Pricing.cs
int[] quantities = [2, 3, 4];
Console.WriteLine($"items:{quantities.Length}");
Console.WriteLine($"total:{Pricing.Total(quantities, unitPrice: 5)}");
Pricing.csDownload file ↗
Pricing.cs
public static class Pricing
{
    public static int Total(int[] quantities, int unitPrice)
    {
        int total = 0;
        foreach (int quantity in quantities) total = checked(total + quantity * unitPrice);
        return total;
    }
}

Expected result

Expected console output, in this order: items:3 total:45 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

Program.cs
using System;
int[] quantities = [2, 3, 4];
Console.WriteLine($"items:{quantities.Length}");
Console.WriteLine($"total:{Pricing.Total(quantities, unitPrice: 5)}");
static class Pricing
{
    public static int Total(int[] quantities, int unitPrice)
    {
        int total = 0;
        foreach (int quantity in quantities) total = checked(total + quantity * unitPrice);
        return total;
    }
}
Open C# practice

C# practice output

items:3
total:45
Try this
The companion keeps Pricing in one file. Externally, remove #:include and read CS0103: a sibling .cs file is not compiled by itself.

Further reading