A fork of bflat compiles C# straight to a small native program: no .NET runtime, no garbage collector, nothing to install where it runs.
You keep C#'s syntax, type checking and generics. Memory comes from arenas you free all at once, and a small library gives you strings, collections, input and output on those terms. The same source runs on Linux, macOS and Windows, on WebAssembly, on DOS on a 386, and on a bare PC with no operating system, started by its UEFI firmware.
// Read into a stack buffer: nothing allocated per answer.
Span<char> buffer = stackalloc char[32];
Out.Write($"Q{i}: {a} + {b} = ");
int length = In.ReadLine(buffer);
// Kept until the end, so it goes into an arena.
mistakes.Add(Str.Format(keep,
$"{a} + {b} = {a + b}, not {value}"));
From the quiz the tutorial builds, step by step.
The language stays, the runtime goes
The compiler still checks everything it checks in .NET. What's missing is what a runtime would do while your program runs.
You keep
- C# 15Unions, extension members and the new memory safety rules.
- Real genericsA specialized copy for each type, and static abstract members.
- Interfaces and virtual methodsDefault interface methods, casts,
isandas. - Records, tuples, anonymous typesValue equality,
with, and the generatedToString. - Lambdas and queries
Where,SelectandGroupBy, from a small library instead of LINQ. - Threads
Parallel.Forruns a loop on every core.
You do without
- Garbage collectorArenas free their memory all at once, when you say.
- .NET runtimeHello world is 11 KB on Windows and 40 KB on Linux, with nothing to install.
- Catching exceptions
throwends the program. Return errors as unions. - Reflection and asyncA type can be compared, but it has no name.
One program, six machines
A new project from dotnet new zero, built for each target by setting one property, ZeroTarget. The repository builds WebAssembly and DOS too, and one test suite requires the same output on every target.
$ dotnet new zero -n Hello $ cd Hello && dotnet build -c Release $ bin/Release/zero03/Hello What's your name? Ada Hello, Ada!
Linux, x64 or arm64
A native program that needs only the C library: 46 KB.
$ dotnet new zero -n Hello $ cd Hello && dotnet build -c Release $ bin/Release/zero03/Hello What's your name? Ada Hello, Ada!
macOS on Apple Silicon
A native Mac program, 66 KB, linked by the package's own linker. No Xcode needed.
> dotnet new zero -n Hello > cd Hello && dotnet build -c Release > bin\Release\zero03\Hello.exe What's your name? Ada Hello, Ada!
Windows x64
A 14 KB console program on Windows' own C library, built on Windows, Linux or a Mac.
dotnet build -p:ZeroTarget=uefi-x64 Shell> fs0: FS0:\> Hello.efi What's your name? Ada Hello, Ada!
UEFI, no operating system
A 15 KB program the PC's firmware starts by itself, from a USB stick. x64 or arm64.
$ wasmtime Minimal.wasm Hello from zero-mode! $ wc -c Minimal.wasm 15833 Minimal.wasm
WebAssembly
A WASI module, through NativeAOT-LLVM. Runs under wasmtime, or in your browser.
C:\>D:
D:\>PROGRAM
Hello from zero-mode!
D:\>DIR PROGRAM.EXE
PROGRAM EXE 16,384
D:\>
DOS on a 386
The same bitcode compiled for a 386, run by the HX DOS extender. Tested on Bochs's i386 model.
What's different, and what you get
Without a runtime, a few things work differently. Each has a page that explains why and what to do instead.
-
Memory is yours to free
There is no garbage collector, so
Memory without a garbage collectornewis never freed. Arenas hand out memory and free it all at once, one per lifetime. -
Text without throwaway strings
Strings without a garbage collectorOut.Line($"...")formats straight into the output buffer, andIn.ReadLinereads into a span. Strings that must last go into an arena. -
Collections that live in arenas
Choose a collectionList,Dict, and their versions for references and string keys, all handles into an arena. -
C# 15, with the safety rules on
Every program compiles under the updated memory safety rules: creating a pointer is safe, reading through one is marked and explained.
The memory safety rules
Where to start
If you're new to it, these four pages take you from nothing to an interactive program.
- Install
With dotnet on Linux, Windows or a Mac, or from the repository for every target.
- Your first program
Build it, run it, see how small it is.
- Run it on DOS
The same program under FreeDOS, in an emulator.
- The tutorial
A quiz: output, input, bad input, arenas.
Looking for something specific? The task guides answer one question each, and the reference lists every type in the library.