Loading...
Loading...
Run single-file C# programs as scripts 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.
npx skill4agent add dotnet/skills csharp-scripts| Input | Required | Description |
|---|---|---|
| C# code or intent | Yes | The code to run, or a description of what the script should do |
dotnet --version.cs.csproj// hello.cs
Console.WriteLine("Hello from a C# script!");
var numbers = new[] { 1, 2, 3, 4, 5 };
Console.WriteLine($"Sum: {numbers.Sum()}");Mainusingdotnet hello.cs--dotnet hello.cs -- arg1 arg2 "multi word arg"#:package#:package Humanizer@2.14.1
using Humanizer;
Console.WriteLine("hello world".Titleize());dotnet clean hello.cs.cs#!/usr/bin/env dotnet
Console.WriteLine("I'm executable!");chmod +x hello.cs./hello.csLFCRLFJsonSerializer.Serialize<T>(value)using System.Text.Json;
using System.Text.Json.Serialization;
var person = new Person("Alice", 30);
var json = JsonSerializer.Serialize(person, AppJsonContext.Default.Person);
Console.WriteLine(json);
var deserialized = JsonSerializer.Deserialize(json, AppJsonContext.Default.Person);
Console.WriteLine($"Name: {deserialized!.Name}, Age: {deserialized.Age}");
record Person(string Name, int Age);
[JsonSerializable(typeof(Person))]
partial class AppJsonContext : JsonSerializerContext;dotnet project convert hello.csmkdir -p /tmp/csharp-script && cd /tmp/csharp-script
dotnet new console -o . --forceProgram.csdotnet rundotnet add package <name>dotnet --versiondotnet build <file>.csdotnet <file>.cs| Pitfall | Solution |
|---|---|
| Move the script outside the project directory, or use |
| Specify a version: |
| Reflection-based JSON serialization fails | Use source-generated JSON with |
| Unexpected build behavior or version errors | File-based apps inherit |