deniz.in

Markets

Weather

Loading weather

· via Hacker News – Front Page (native)

Zero C# compiles C# to tiny runtime-free binaries, from Linux to WebAssembly to DOS

Zero C#, a bflat compiler fork featured on Hacker News, builds C# programs into small native binaries with no .NET runtime or garbage collector, targeting Linux, macOS, Windows, WebAssembly and even DOS on a 386.

Zero C# compiles C# to tiny runtime-free binaries, from Linux to WebAssembly to DOS

A project called Zero C# reached Hacker News's front page on 21 November with an unusual pitch: C# compiled directly into small native programs, with no .NET runtime and no garbage collector shipped alongside the code. According to the project's page, linked from the Show HN post, it is a fork of the bflat compiler that leaves the language itself intact while removing almost everything that normally executes underneath it.

What the compiler keeps, and what it drops

The documentation says the compiler still performs the static checking .NET developers rely on, and supports modern language features. On the kept side: C# 15 constructs including unions, extension members and the updated memory safety rules; generics that generate a specialized copy for each type, with support for static abstract members; interfaces with default method implementations, virtual dispatch and is/as casts; records, tuples and anonymous types with their usual equality and ToString behaviour; and lambdas, with Where, Select and GroupBy-style query operators supplied by a small bundled library rather than LINQ itself. Threads survive as well, with Parallel.For able to spread a loop across every available core.

The dropped side is equally specific. There is no garbage collector, so memory allocated with new is never reclaimed automatically; arenas are released in a single block when the programmer decides. There is no runtime to install either, and the project reports a hello-world executable of 10,752 bytes on Windows and 39,848 bytes on Linux. Exceptions cannot be caught, meaning a throw simply ends the program, and errors are expected to be returned as unions instead. Reflection and async are also gone.

One hello world, five platforms

The project's central demonstration is a single hello-world file, left unchanged, built for five environments. Each build is checked by a shared test suite that requires identical output on every target.

  • Linux on x64 or arm64: a native program depending only on the C library.
  • macOS on Apple Silicon: bflat compiles it and the Mac's own clang links it, with no container required; a container-based build path also exists. The binary is 64,728 bytes.
  • Windows x64: a console program that can be built from Linux or macOS and is tested under Wine, at 10,752 bytes.
  • WebAssembly: a WASI module produced through NativeAOT-LLVM and run under wasmtime, at 15,833 bytes.
  • DOS on a 386: the same compiled bitcode executed through the HX DOS extender, verified on Bochs's i386 model, producing a 16,384-byte executable.

Memory without a collector

The fork is paired with a small library built around arena allocation. Formatted output through Out.Line writes directly into the output buffer, and In.ReadLine reads into a caller-provided span, so the tutorial's quiz example answers questions without allocating anything per response, using a stack-allocated character buffer. Strings that must outlive a call are placed in an arena instead. Collections, including List and Dict plus variants for references and string keys, are handles into an arena rather than standalone objects.

The documentation also notes that every program compiles under the updated C# memory safety rules, where creating a pointer is treated as safe but reading through one is explicitly marked and explained. Getting-started material covers installing the fork, building a first program, running it under FreeDOS in an emulator, and a tutorial that constructs an interactive quiz covering output, input, malformed input and arena use.

Why it matters

Managed runtimes have long been the cost of admission for languages like C#, and ahead-of-time compilation efforts typically shrink that footprint while still carrying a runtime and a garbage collector in some form. Zero C# removes both, which makes C# a plausible candidate in places it previously could not reach: minimal WebAssembly modules, stripped-down containers, and hardware old enough to predate the .NET ecosystem entirely.

The trade is a programming model closer to C or Rust, with explicit lifetime discipline, error returns instead of exceptions, and no reflection or async, all wrapped in C#'s syntax and type system. As a community-shared fork from an individual developer, it is early work rather than a production toolchain, and the lack of exception handling and async will rule out many existing codebases. But as a demonstration of how far native compilation of C# can be pushed toward constrained environments, it is a striking data point, and the multi-platform test discipline behind even its DOS target suggests the experiment is being run carefully.

  • #c-sharp
  • #compilers
  • #native-aot
  • #webassembly
  • #embedded