Conversations
Zero C# explores native AOT compilation of C# without runtime or garbage collection
The Show HN project Zero C# is a fork of bflat that can compile C# directly into small native programs without the .NET runtime, garbage collection, or an environment installed on the target machine. It retains C# syntax, type checking, generics, interfaces, records, lambdas, and queries, among other capabilities; memory is released uniformly through arenas, and it can target Linux, macOS, Windows, WebAssembly, and 386 DOS.
Zero C#’s goal is for a C# program to have “nothing underneath.” Based on a bflat fork, it compiles C# source code directly into small native executables, with no .NET runtime required at the execution location and no garbage collector. Developers can still use C# syntax, compile-time type checking, and generics; memory comes from arenas that can be released all at once, and a small library provides strings, collections, and input/output capabilities.
The project retains many modern C# features, including C# 15 unions, extension members, and new memory-safety rules, true generics, static abstract members, interfaces, virtual methods, default interface methods, type conversions, records, tuples, anonymous types, lambdas, and query capabilities such as Where, Select, and GroupBy provided by a small library. It also supports running loops across multiple cores with Parallel.For.
What has been removed or changed is the runtime mechanism: there is no garbage collector, and memory allocated with new is not automatically released, requiring arena-based reclamation according to lifetime; there is no .NET runtime, with a Windows hello world at about 11KB and Linux at about 40KB; code cannot rely on catching exceptions, and throw terminates the program, with errors returned through unions; reflection and async are also unavailable, and types can be compared but have no names.
Cross-platform support is a highlight of the project. The same hello-world source can be built for Linux x64 or arm64, Apple Silicon macOS, Windows x64, WASI WebAssembly, and 386 DOS running through the HX DOS extender. Example artifact sizes listed in the material include 64,728 bytes for macOS, 15,833 bytes for WASM, and 16,384 bytes for the DOS program, with each target checked for output consistency by the same test suite. The project also explains that in a GC-free environment, string output can be written directly to an output buffer, input can be read into a Span, and strings and collections that need to be retained long term are placed in an arena.