← Dolaima← Docs indexC#原文中文

C

Reads like desktop C#; the entry point is always a class named P.

How to run it on your phone

vml run examples/csharp/test/sysinfo.cs

Compiling takes a while (one to two minutes for C, a few seconds for scripting languages). Once the program is running, the gamepad is at the bottom of the screen - the D-pad and the four action buttons are all there. Tap the back arrow in the top-right corner to return to the Shell.

What to know before you write

Examples

FileWhat it shows
app/racer.csRacing
app/snake.csSnake
test/file_io.csReading and writing files
test/sysinfo.csDevice information

Also in this folder: app/breakout.cs, app/g2048.cs, demo/demo_std.cs, test/libs.cs, tty/demo_tty.cs, ui/demo_ui.cs


The rest of this page is taken straight from the VML source (third_party/vml/VMLPrepares/CSharpCompiler/): the README covers what this front end supports and how to compile it, and the language reference covers the syntax itself. When upstream changes, regenerating this page brings it up to date.

Language reference

C# language compiler specification

Version: v1.0 | Date: 2026-07-06 | Revised by: Shenzhen Tanso Intelligent Technology Co., Ltd.

Specification standard

FieldValue
Target standardC# 5.0 subset (ECMA-334:2012)
Publication year2012
Completeness~92%
MCU completeness~90%
Tests0 (test directory yet to be created)
Updates2026-05-18: corrected the target standard version, completeness and test counts

Keywords

abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while

Overview

A subset of the C# language is supported, compiled to VML assembly. The architecture is statically compiled. The target features described in this document include C# 2.0~5.0+ syntax (LINQ, async/await, switch expressions and so on); the actual implementation is centred on basic syntax, with the advanced features yet to be implemented.

Supported language features

1. Data types
2. Variables and constants
int number = 10;
const double PI = 3.14159;
string text = "Hello";
var inferred = 42;  // type inference
int? nullable = null; // nullable type
3. Control flow
// if-else
if (condition) {
    Console.WriteLine("true");
} else {
    Console.WriteLine("false");
}

// switch expression
string result = value switch {
    1 => "One",
    2 => "Two",
    _ => "Other"
};

// loops
for (int i = 0; i < 10; i++) { }
foreach (var item in collection) { }
while (condition) { }
do { } while (condition);
4. Methods and properties
public int Add(int a, int b) {
    return a + b;
}

// properties
public string Name {
    get => _name;
    set => _name = value;
}

// auto-property
public int Age { get; set; }

// lambda expression
Func<int, int> square = x => x * x;
5. Classes and interfaces
public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    
    public Person(string name, int age) {
        Name = name;
        Age = age;
    }
    
    public virtual void Introduce() {
        Console.WriteLine($"I'm {Name}, {Age} years old");
    }
}

public interface IDrawable {
    void Draw();
}
6. Exception handling
try {
    int result = Divide(10, 0);
} catch (DivideByZeroException ex) {
    Console.WriteLine($"除零错误: {ex.Message}");
} catch (Exception ex) {
    Console.WriteLine($"其他错误: {ex.Message}");
} finally {
    Console.WriteLine("清理资源");
}

// custom exception
public class CustomException : Exception {
    public int ErrorCode { get; }
    
    public CustomException(string message, int errorCode) 
        : base(message) {
        ErrorCode = errorCode;
    }
}
7. LINQ and collections
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
var doubled = numbers.Select(n => n * 2);
var sum = numbers.Sum();
var average = numbers.Average();

// dictionary
var dict = new Dictionary<string, int> {
    ["apple"] = 1,
    ["banana"] = 2
};
8. Asynchronous programming
public async Task<string> FetchDataAsync() {
    using var client = new HttpClient();
    return await client.GetStringAsync("https://api.example.com");
}

// using async/await
async Task ProcessData() {
    try {
        string data = await FetchDataAsync();
        Console.WriteLine(data);
    } catch (HttpRequestException ex) {
        Console.WriteLine($"网络错误: {ex.Message}");
    }
}

Floating-point and 64-bit compile modes

The VML toolchain controls how floating-point values and 64-bit integers are handled through three compile options:

OptionValuesDefaultDescription
--float32hard / soft / nonehardHow 32-bit floating point (float) is handled
--float64hard / soft / nonesoftHow 64-bit floating point (double) is handled
--int64hard / soft / nonesoftHow 64-bit integers (long/ulong) are handled
32-bit floating point (float32)

The 32-bit single-precision floating-point type float in this language is compiled in one of the following modes:

64-bit floating point (double)

The 64-bit double-precision floating-point type double in this language is compiled in one of the following modes:

64-bit integers (int64)

The 64-bit integer types long / ulong in this language are compiled in one of the following modes:

Software simulation libraries

The software simulation libraries above all live in the Lib/shared/ directory. They are written in C and compiled to VML by the C compiler, and every language shares them:

Library filePurposeCore functions
softfloat.cQ15.16 fixed-point 32-bit floating-point simulation__vml_float_add/sub/mul/div/neg/abs/cmp, __vml_int2float/float2int
softdouble.cIEEE 754 double-precision 64-bit floating-point simulation__vml_double_add/sub/mul/div/neg/abs/cmp, __vml_int2double/double2int, __vml_float2double/double2float
softint64.c64-bit integer double-register simulation__vml_i64_add/sub/neg/and/or/xor/not/shl/shr

Compiler status

Compile and use

dotnet run --project VMLTool -- input.cs -o output.vml
# or plugin mode:
vmltool input.cs -o output.vml

🆕 String type (v1.65.19)

This language compiler stores strings internally as .wstring (UTF-16LE) by default.

ModeDefault encodingVML directiveSYSCALL output
MCU (default).string (UTF-8).string#1
OS.wstring (UTF-16LE).wstring#391

Predefined macro: VML_WSTRING — defined automatically in OS mode, undefined in MCU mode Output function: OS mode automatically uses shared_print_wstr (converts UTF-16LE to UTF-8 automatically)

// user code can test the encoding with the macro
#ifdef VML_WSTRING
  // the default string is wstring (UTF-16LE)
#else
  // the default string is UTF-8
#endif

Compiler README

C# compiler

Path: VMLPrepares/CSharpCompiler/ Completeness: ~92% | 🟢 production ready Standard library: Lib/csharp/ (to be created)

Features

Compile modes

MCU mode (default, --mode mcu)

MCU mode is tuned for microcontroller and bare-metal environments (Arduino/STM32/8051 and the like) and automatically skips features that need an operating system.

Skipped (no code is generated when these appear):

Kept (the low-level parts are provided by the BIOS):

OS mode (--mode os, reserved)

OS mode targets environments that have an operating system (embedded Linux, RTOS and so on). It will then support the full set of language features (file system, threads, async, exceptions, reflection and so on).

RAM level
MCU safe-coding tips

Usage

dotnet run --project VMLPrepares/CSharpCompiler input.CS -o output.vml

Tests

Test/CS/ — 0 test files (the test directory is yet to be created)