Zero C#

C# compiled to small native programs with no .NET runtime and no garbage collector. On Linux a hello
world is 14 KB. It builds on bflat's zero mode, uses C# 15 with the updated
memory safety rules, and runs on Linux, macOS, Windows, WebAssembly, DOS on a 386, and UEFI with no
operating system at all. Programs that are small enough run on a Commodore 64.
You keep C#'s syntax, generics, spans, interfaces and lambdas. You lose the garbage collector,
catchable exceptions, reflection and almost all of the base class library. Memory you want back goes in
an arena and is freed all at once.
Documentation: https://onurgumus.github.io/zero-csharp/
Try it
With the .NET 10 SDK on Linux, Windows or an Apple Silicon Mac:
dotnet new install ZeroCSharp.Templates
dotnet new zero -o Hello
cd Hello
dotnet build
That gives you a native program in bin/Debug/zero03/. The template's Program.cs:
using System;
Console.Write("What's your name? ");
Span<char> buffer = stackalloc char[64];
int length = In.ReadLine(buffer);
ReadOnlySpan<char> name = length switch
{
<= 0 => "world",
_ when length > buffer.Length => buffer,
_ => buffer.Slice(0, length),
};
Console.Write("Hello, ");
Console.Write(name);
Console.WriteLine("!");
Add -p:ZeroTarget=win-x64 (or win-arm64, linux-x64, linux-arm64, osx-arm64, uefi-x64, uefi-arm64) to
build for another system, or c64, pico2 (Raspberry Pi Pico 2) or r4 (Arduino Uno R4) for a
machine without one; dotnet new zeropico starts a microcontroller project.
A library project with <NativeLib>Shared</NativeLib> builds a .so, .dylib or .dll that .NET and
C programs can call. The project comes with a VS Code setup for debugging.
On an Apple Silicon Mac, dotnet new zerogpu makes a program whose C# kernels run on the GPU, through
Metal (<ZeroGpu>Metal</ZeroGpu>; it needs Xcode, its Metal Toolchain and Docker).
More: Coming from .NET,
Install with dotnet,
the tutorial,
Build a native library,
Debug a program.
For Claude Code, this repository is a plugin marketplace whose plugin teaches it zero mode's rules
and where to read the docs: /plugin install zero-csharp --marketplace OnurGumus/zero-csharp
(Write programs with Claude Code).
Targets
| Target | Build from this repo | Program sizes |
|---|
| Linux x64 and arm64 | ./build.sh | 13 KB (Tiny) to 139 KB (Collections) |
| macOS, Apple Silicon | ./build.sh --mac, on a Mac | from 50 KB |
| iOS simulator, Apple Silicon | ./build.sh --ios, on a Mac with Xcode | from 50 KB |
| Android arm64 (command-line programs) | ./build.sh --android | 15 KB (Tiny) to 148 KB (Collections) |
| macOS with C# kernels on the GPU (Metal), experimental | ./build.sh --gpu, on a Mac | 181 KB (the Gpu sample) |
| Windows x64 | ./build.sh --win, from Linux or macOS | 17 KB (Tiny) to 127 KB (Collections) |
| Windows arm64 | ./build.sh --win-arm64, from Linux or macOS | 14 KB (Tiny) to 135 KB (Collections) |
| WebAssembly (WASI) | ./build.sh --wasm | 17 KB (Tiny) to 178 KB (Collections) |
| DOS, 386 with a 387 | ./build.sh --dos | 22 KB (Tiny) to 202 KB (Collections) |
| UEFI x64 and arm64, no OS | ./build.sh --uefi | 12 KB (Tiny) to 130 KB (Collections) |
| Commodore 64 | ./build.sh --c64 | 6 KB to 47 KB, of the 50 KB there are |
Commander X16 (65C02, 38 KB of fixed RAM), in x16emu, with lib/CX16.cs and more: text, graphics, sprites, mouse, sound; community-maintained | ./build.sh --cx16 | 5 KB to 34 KB |
| Raspberry Pi Pico 2 (Cortex-M33), in QEMU | ./build.sh --pico2 | 8 KB (Tiny) to 138 KB (Collections) of flash |
| Arduino Uno R4 (Cortex-M4, 32 KB of RAM), in QEMU | ./build.sh --r4 | 8 KB (Tiny) to 214 KB (Guards) of flash |