{
  "schema": "https://ai-atoms.com/schemas/skill-v1.json",
  "type": "skill",
  "id": "skill/csharp-scripts",
  "version": "1.0.0",
  "name": "csharp-scripts",
  "description": "Run file-based C# apps with the .NET CLI when the user explicitly wants C#/.NET code without creating a project. Use for C# language/API experiments, one-file C# apps, small multi-file C# apps composed with `#:include`/`#:exclude`, or C# file-based apps linked with `#:ref`. Do not use for language-agnostic throwaway scripts, generic computations, Python/PowerShell-style automation, full projects, or existing app integration.",
  "system_prompt_fragment": "# File-Based C# Apps\n\n## When to Use\n\n- Testing a C# concept, API, or language feature with a quick file-based app\n- Prototyping logic before integrating it into a larger project\n- Building a small utility from one entry-point file and a few helper `.cs` files\n\n## When Not to Use\n\n- The user asks for a language-agnostic quick script, throwaway computation, or shell/Python/PowerShell-style automation\n- The user needs a full project, solution integration, or project references in an existing app\n- The user is working inside an existing .NET solution and wants to add code there\n- The app is large enough that project structure, build customization, tests, or publish configuration should live in a `.csproj`\n\n## Inputs\n\n| Input | Required | Description |\n|-------|----------|-------------|\n| C# code or intent | Yes | The code to run, or a description of what the file-based app should do |\n\n## Workflow\n\n### Step 1: Check the .NET SDK version\n\nRun `dotnet --version` to verify the SDK is installed and note the full version, including the feature band. File-based apps require .NET 10 or later. `#:include`, `#:exclude`, and transitive directive processing require SDK 10.0.300 or later; SDK 10.0.100/10.0.200 builds can run single-file apps but do not support those multi-file directives. If the version is below 10, follow the [fallback for older SDKs](#fallback-for-net-9-and-earlier) instead.\n\n### Step 2: Write the app file\n\nCreate an entry-point `.cs` file using top-level statements. Place it outside any existing project directory to avoid conflicts with `.csproj` files.\n\n```csharp\n#!/usr/bin/env dotnet\n// hello.cs\nConsole.WriteLine(\"Hello from a file-based app!\");\n\nvar numbers = new[] { 1, 2, 3, 4, 5 };\nConsole.WriteLine($\"Sum: {numbers.Sum()}\");\n```\n\nGuidelines:\n\n- Use top-level statements (no `Main` method, class, or namespace boilerplate)\n- Place `using` directives at the top of the file (after the `#!` line and any `#:` directives if present)\n- Place type declarations (classes, records, enums) after all top-level statements\n\n### Step 3: Run the app\n\n```bash\ndotnet hello.cs\n```\n\nBuilds and runs the file automatically. Cached so subsequent runs are fast. Pass arguments after `--`:\n\n```bash\ndotnet hello.cs -- arg1 arg2 \"multi word arg\"\n```\n\n### Step 4: Add directives (if needed)\n\nPlace 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 `#:`.\n\n#### `#:package` — NuGet package references\n\nSpecify a version unless the app intentionally uses central package management. Use `@*` when the latest available package is acceptable (or `@*-*` for pre-release):\n\n```csharp\n#:package Humanizer@2.14.1\n\nusing Humanizer;\n\nConsole.WriteLine(\"hello world\".Titleize());\n```\n\n#### `#:property` — MSBuild properties\n\nSet any MSBuild property inline. Syntax: `#:property PropertyName=Value`\n\n```csharp\n#:property AllowUnsafeBlocks=true\n#:property PublishAot=false\n#:property NoWarn=CS0162\n```\n\nMSBuild expressions and property functions are supported:\n\n```csharp\n#:property LogLevel=$([MSBuild]::ValueOrDefault('$(LOG_LEVEL)', 'Information'))\n```\n\nCommon properties:\n\n| Property | Purpose |\n|----------|---------|\n| `AllowUnsafeBlocks=true` | Enable `unsafe` code |\n| `PublishAot=false` | Disable native AOT (enabled by default) |\n| `NoWarn=CS0162;CS0219` | Suppress specific warnings |\n| `LangVersion=preview` | Enable preview language features |\n| `InvariantGlobalization=false` | Enable culture-specific globalization |\n\n#### `#:project` — Project references\n\nReference another project by relative path:\n\n```csharp\n#:project ../MyLibrary/MyLibrary.csproj\n```\n\n#### `#:ref` — File-based app references\n\nReference another `.cs` file as a separate file-based app project when it should compile into a separate assembly instead of being included in the same compilation. Use `#:include` for ordinary helper files that should share the same assembly as the entry point; use `#:ref` when you want project-reference-like boundaries.\n\n```csharp\n#:property ExperimentalFileBasedProgramEnableRefDirective=true\n#:ref ../Shared/Formatter.cs\n\nConsole.WriteLine(Formatter.Title(\"hello world\"));\n```\n\nGuidelines:\n\n- The referenced file is compiled as its own virtual project and added as a project reference.\n- If the referenced file is a library without top-level statements, put `#:property OutputType=Library` in that referenced file.\n- Members that must be consumed by the referencing app should be public; internal members are not visible across the assembly boundary.\n- `#:ref` is transitive: a referenced file can contain its own `#:ref` and other `#:` directives.\n- Relative paths are resolved relative to the file containing the directive.\n- Some SDK builds require `#:property ExperimentalFileBasedProgramEnableRefDirective=true`; remove that property if the SDK accepts `#:ref` without it.\n\n#### `#:sdk` — SDK selection\n\nOverride the default SDK (`Microsoft.NET.Sdk`):\n\n```csharp\n#:sdk Microsoft.NET.Sdk.Web\n```\n\n#### `#:include` and `#:exclude` — Multi-file apps\n\nIn .NET SDK 10.0.300 and later, file-based apps can include additional files in the same virtual project. Check the full `dotnet --version` output before using these directives; a 10.0.100 or 10.0.200 SDK is still .NET 10 but does not support them. Use `#:include` for helper source files and supported assets, and `#:exclude` to remove files from an include pattern or default item set.\n\n```csharp\n#!/usr/bin/env dotnet\n#:include Helpers.cs\n#:include Models/*.cs\n#:exclude Models/Generated/*.cs\n\nConsole.WriteLine(Formatter.Title(\"hello world\"));\n```\n\nGuidelines:\n\n- Treat the file passed to `dotnet` as the entry point; put top-level statements there.\n- Put declarations such as classes, records, and enums in included `.cs` files.\n- Prefer explicit globs such as `Helpers.cs` or `Models/*.cs` over broad recursive globs.\n- Paths are resolved relative to the file containing the directive.\n- Include directives from non-entry-point C# files are processed too, so a helper file can declare its own `#:package`, `#:property`, `#:sdk`, `#:project`, `#:ref`, `#:include`, or `#:exclude` directives.\n- Avoid duplicate directives across included files unless the directive kind explicitly supports duplicates; duplicate `#:package`, `#:property`, `#:sdk`, `#:include`, and `#:exclude` entries can fail.\n- When an app uses `#:include`, add a shebang (`#!/usr/bin/env dotnet`) to the entry-point file on Unix-like systems to make the entry point clear to tools. Use `LF` line endings and no BOM for shebang files.\n\nExample layout:\n\n```text\nscratch/\n    hello.cs\n    Helpers.cs\n    Models/\n        Person.cs\n```\n\n```csharp\n#!/usr/bin/env dotnet\n// hello.cs\n#:include Helpers.cs\n#:include Models/*.cs\n\nvar person = new Person(\"Ada\");\nConsole.WriteLine(Formatter.Title(person.Name));\n```\n\n```csharp\n// Helpers.cs\nstatic class Formatter\n{\n    public static string Title(string value) => value.ToUpperInvariant();\n}\n```\n\n```csharp\n// Models/Person.cs\nrecord Person(string Name);\n```\n\n### Step 5: Clean up\n\nRemove the app files when the user is done. To clear cached build artifacts:\n\n```bash\ndotnet clean hello.cs\n```\n\n## Unix shebang support\n\nOn Unix platforms, make a `.cs` file directly executable:\n\n1. Add a shebang as the first line of the file:\n\n    ```csharp\n    #!/usr/bin/env dotnet\n    Console.WriteLine(\"I'm executable!\");\n    ```\n\n2. Set execute permissions:\n\n    ```bash\n    chmod +x hello.cs\n    ```\n\n3. Run directly:\n\n    ```bash\n    ./hello.cs\n    ```\n\nUse `LF` line endings (not `CRLF`) when adding a shebang. This directive is ignored on Windows.\n\n## Source-generated JSON\n\nFile-based apps enable native AOT by default. Reflection-based APIs like `JsonSerializer.Serialize<T>(value)` fail at runtime under AOT. Use source-generated serialization instead:\n\n```csharp\nusing System.Text.Json;\nusing System.Text.Json.Serialization;\n\nvar person = new Person(\"Alice\", 30);\nvar json = JsonSerializer.Serialize(person, AppJsonContext.Default.Person);\nConsole.WriteLine(json);\n\nvar deserialized = JsonSerializer.Deserialize(json, AppJsonContext.Default.Person);\nConsole.WriteLine($\"Name: {deserialized!.Name}, Age: {deserialized.Age}\");\n\nrecord Person(string Name, int Age);\n\n[JsonSerializable(typeof(Person))]\npartial class AppJsonContext : JsonSerializerContext;\n```\n\n## Converting to a project\n\nWhen a file-based app outgrows this workflow, convert it to a full project:\n\n```bash\ndotnet project convert hello.cs\n```\n\n## Fallback for .NET 9 and earlier\n\nIf the .NET SDK version is below 10, file-based apps are not available. Use a temporary console project instead:\n\n```bash\nmkdir -p /tmp/csharp-file-based-app && cd /tmp/csharp-file-based-app\ndotnet new console -o . --force\n```\n\nReplace the generated `Program.cs` with the app content and run with `dotnet run`. Add NuGet packages with `dotnet add package <name>`. Remove the directory when done.\n\n## Validation\n\n- [ ] `dotnet --version` reports 10.0 or later (or fallback path is used)\n- [ ] If the app uses `#:include`, `#:exclude`, or transitive directives from included files, `dotnet --version` reports SDK 10.0.300 or later\n- [ ] The app compiles without errors (can be checked explicitly with `dotnet build <file>.cs`)\n- [ ] `dotnet <file>.cs` produces the expected output\n- [ ] Multi-file apps include every required helper file and exclude unintended matches\n- [ ] App files and cached artifacts are cleaned up after the session\n\n## Common Pitfalls\n\n| Pitfall | Solution |\n|---------|----------|\n| `.cs` file is inside a directory with a `.csproj` | Move the app outside the project directory, or use `dotnet run --file file.cs` |\n| `#:package` without a version | Specify a version: `#:package PackageName@1.2.3` or `@*` for latest |\n| `#:property` with wrong syntax | Use `PropertyName=Value` with no spaces around `=` and no quotes: `#:property AllowUnsafeBlocks=true` |\n| Directives placed after C# code | All `#:` directives must appear immediately after an optional shebang line (if present) and before any `using` directives or other C# statements |\n| Helper file is not compiled | Add `#:include Helper.cs` or an appropriate glob to the entry-point file |\n| Shared file needs an assembly boundary | Use `#:ref Shared.cs` instead of `#:include Shared.cs`, and set `#:property OutputType=Library` in the referenced file if it has no entry point |\n| Broad include pulls in unrelated files | Prefer narrow include patterns and use `#:exclude` for generated, backup, or experimental files |\n| Duplicate directives in included files | Keep package, property, SDK, include, and exclude directives unique across the entry point and included C# files |\n| Reflection-based JSON serialization fails | Use source-generated JSON with `JsonSerializerContext` (see [Source-generated JSON](#source-generated-json)) |\n| Unexpected build behavior or version errors | File-based apps inherit `global.json`, `Directory.Build.props`, `Directory.Build.targets`, and `nuget.config` from parent directories. Move the app to an isolated directory if the inherited settings conflict |\n\n## More info\n\nSee https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps for a full reference on file-based apps.",
  "applicable_domains": [
    "code",
    "dotnet",
    "engineering"
  ],
  "invocation": [
    "/csharp-scripts"
  ],
  "tags": [
    "dotnet",
    "csharp",
    "microsoft"
  ],
  "authored_by": "anthropics",
  "source_url": "https://github.com/dotnet/skills/blob/main/plugins/dotnet/skills/csharp-scripts/SKILL.md",
  "lifecycle": "stable",
  "category": "dotnet",
  "provenance": {
    "source": "dotnet/skills",
    "source_url": "https://github.com/dotnet/skills/blob/main/plugins/dotnet/skills/csharp-scripts/SKILL.md",
    "author": "Microsoft / .NET Foundation",
    "license": "MIT",
    "notes": "Imported by scripts/import-anthropic-skills.py."
  }
}