Platform Core v0.1.0

C# Scripts

Run single-file C# programs as scripts (file-based apps) for quick experimentation, prototyping, and concept testing. Use when the user wants to write and execute a small C# program without creating a full project.

Workflow

Step 1: Check the .NET SDK version

Run dotnet --version to verify the SDK is installed and note the major version number. File-based apps require .NET 10 or later. If the version is below 10, follow the fallback for older SDKs instead.

Step 2: Write the script file

Create a single .cs file using top-level statements. Place it outside any existing project directory to avoid conflicts with .csproj files.

// hello.cs
Console.WriteLine("Hello from a C# script!");

var numbers = new[] { 1, 2, 3, 4, 5 };
Console.WriteLine($"Sum: {numbers.Sum()}");

Guidelines:

  • Use top-level statements (no Main method, class, or namespace boilerplate)
  • Place using directives at the top of the file (after the #! line and any #: directives if present)
  • Place type declarations (classes, records, enums) after all top-level statements

Step 3: Run the script

dotnet hello.cs

Builds and runs the file automatically. Cached so subsequent runs are fast. Pass arguments after --:

dotnet hello.cs -- arg1 arg2 "multi word arg"

Step 4: Add directives (if needed)

Place directives at the top of the file (immediately after an optional shebang line), before any using directives or other C# code. All directives start with #:.

#### #:package — NuGet package references

Always specify a version:

#:package Humanizer@2.14.1

using Humanizer;

Console.WriteLine("hello world".Titleize());

#### #:property — MSBuild properties

Set any MSBuild property inline. Syntax: #:property PropertyName=Value

#:property AllowUnsafeBlocks=true
#:property PublishAot=false
#:property NoWarn=CS0162

MSBuild expressions and property functions are supported:

#:property LogLevel=$([MSBuild]::ValueOrDefault('$(LOG_LEVEL)', 'Information'))

Common properties:

| Property | Purpose | |----------|---------| | AllowUnsafeBlocks=true | Enable unsafe code | | PublishAot=false | Disable native AOT (enabled by default) | | NoWarn=CS0162;CS0219 | Suppress specific warnings | | LangVersion=preview | Enable preview language features | | InvariantGlobalization=false | Enable culture-specific globalization |

#### #:project — Project references

Reference another project by relative path:

#:project ../MyLibrary/MyLibrary.csproj

#### #:sdk — SDK selection

Override the default SDK (Microsoft.NET.Sdk):

#:sdk Microsoft.NET.Sdk.Web

Step 5: Clean up

Remove the script file when the user is done. To clear cached build artifacts:

dotnet clean hello.cs

Related skills

Use ManagedCode.Communication when a .NET application needs explicit result objects, structured errors, and predictable service or API boundaries instead of exception-driven…

ManagedCode.Communication

Use ManagedCode.MimeTypes when a .NET application needs consistent MIME type detection, extension mapping, and content-type decisions for uploads, downloads, or HTTP responses.

ManagedCode.MimeTypes

Use the Microsoft.Extensions stack correctly across Generic Host, dependency injection, configuration, logging, options, HttpClientFactory, and other shared infrastructure…

Microsoft.Extensions.*