All tutorialsTutorial

Simple Guide: dotnet run files

Quick start guide for running C# files directly with `dotnet run`.

Claude Code Knowledge Pack7/10/2026

Simple Guide: dotnet run files

Quick start guide for running C# files directly with dotnet run.

Stdin Execution (One-liners)

Perfect for Claude Code and quick scripts:

# Basic output
echo 'Console.WriteLine("Hello");' | dotnet run -

# Math calculation
echo 'Console.WriteLine(Math.Sqrt(144));' | dotnet run -

# Date/time
echo 'Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));' | dotnet run -

# Environment info
echo 'Console.WriteLine(Environment.OSVersion);' | dotnet run -

Multi-line with Heredoc

dotnet run - << 'EOF'
var numbers = new[] { 1, 2, 3, 4, 5 };
var sum = numbers.Sum();
Console.WriteLine($"Sum: {sum}");
EOF

Using NuGet Packages

Add #:package directive:

# Humanizer for text manipulation
dotnet run - << 'EOF'
#:package Humanizer@*
using Humanizer;
Console.WriteLine(TimeSpan.FromMinutes(90).Humanize());
EOF

# JSON serialization (built-in, no package needed)
dotnet run - << 'EOF'
using System.Text.Json;
var data = new { Name = "Test", Value = 42 };
Console.WriteLine(JsonSerializer.Serialize(data));
EOF

Common Use Cases

Quick HTTP Request

dotnet run - << 'EOF'
var client = new HttpClient();
var response = await client.GetStringAsync("https://httpbin.org/get");
Console.WriteLine(response);
EOF

Read and Process File

dotnet run - << 'EOF'
var lines = File.ReadAllLines("data.txt");
Console.WriteLine($"Lines: {lines.Length}");
foreach (var line in lines.Take(5))
    Console.WriteLine(line);
EOF

JSON Processing

dotnet run - << 'EOF'
using System.Text.Json;

var json = """{"name":"Alice","age":30}""";
var doc = JsonDocument.Parse(json);
Console.WriteLine(doc.RootElement.GetProperty("name").GetString());
EOF

Generate GUID

echo 'Console.WriteLine(Guid.NewGuid());' | dotnet run -

Directives Quick Reference

DirectivePurposeExample
#:packageAdd NuGet package#:package Humanizer@2.14.1
#:sdkSet SDK type#:sdk Microsoft.NET.Sdk.Web
#:propertyMSBuild property#:property LangVersion preview
#:projectProject reference#:project ../MyLib

Directives Reference

Package References (#:package)

Version is required. Use wildcards for flexibility:

#:package Humanizer@*                  // Latest version
#:package Dapper@2.*                   // Latest 2.x
#:package Newtonsoft.Json@13.0.3       // Specific version

SDK Selection (#:sdk)

#:sdk Microsoft.NET.Sdk              // Default console
#:sdk Microsoft.NET.Sdk.Web          // ASP.NET Core
#:sdk Microsoft.NET.Sdk.Worker       // Worker services
#:sdk Aspire.AppHost.Sdk@9.0.0       // .NET Aspire

MSBuild Properties (#:property)

#:property LangVersion preview
#:property Nullable enable
#:property ImplicitUsings enable
#:property UserSecretsId my-app-secrets

Project References (#:project)

#:project ../src/MyLibrary
#:project ../src/MyLibrary/MyLibrary.csproj

Tips

  • Use single quotes around heredoc delimiter ('EOF') to prevent shell expansion
  • Directives must be at the top of the file/input
  • Top-level statements work by default (no Main method needed)
  • await works at top level for async operations