---
title: "Build a C# UI component without HTML"
url: https://recursiveai.org/oneui/cookbook/build-ui-component-without-markup/
description: "Build a OneUI button and update its label in pure C#. The example uses Component, Scope and SetState without an HTML template or generated bindings."
version: "1.0.0"
---

# Build a C# UI component without HTML

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in desktop windows or in a browser via WebAssembly with OneApp. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/build-ui-component-without-markup/build-ui-component-without-markup.zip)

```shell
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```cs
using OneUI.Components;
sealed class Greeting : Component
{
    protected override void Build(
        Scope scope, int parent, int before)
        => scope.Insert(parent,
            scope.Text("Hello"), before);
}
```

[The example](https://recursiveai.org/oneui/cookbook/build-ui-component-without-markup/build-ui-component-without-markup.zip) is the full program below: unzip it and `dotnet run`.

This component inserts the text `Hello` without an HTML or CSS file. To try it, use the runner below and mount `new Greeting()` instead of `new Counter()`, then print `doc.ToHtml()` rather than querying or clicking a button.

## The full program

The example extends the same pattern to a button that updates its label. Replace the default `Program.cs` with the runner below. It mounts `Counter` in an in-memory document, reads the initial label, and clicks the button.

`Counter.cs`:

```cs
using OneUI.Components;

sealed class Counter : Component
{
    int _count;
    const ulong CountBit = 1;

    // 0 is the state slot index.
    // CountBit = 1 << 0 is its mask.
    void Increment()
        => SetState(ref _count, _count + 1, 0);

    protected override void Build(
        Scope scope, int parent, int before)
    {
        int button = scope.Element("button");
        int label = scope.Text(CountBit,
            () => $"Count: {_count}");
        scope.Listen<MouseEvent>(
            button, "click", _ => Increment());
        scope.Insert(button, label);
        scope.Insert(parent, button, before);
    }
}
```

`Program.cs`:

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var doc = new ReferenceDocument();
var ui = new UiContext(doc);
doc.Context = ui;
ui.Mount(new Counter());
Console.WriteLine(doc.Text("button"));
doc.Click("button");
Console.WriteLine(doc.Text("button"));
```

`dotnet run` prints:

```text
Count: 0
Count: 1
```

If you create a node but don’t insert it, it stays detached and won’t appear in the mounted component.

## Variations

For a counter that adds five on each click, replace `Increment`:

```csharp
void Increment()
    => SetState(ref _count, _count + 5, 0);
```

## Related recipes

- [Pass HTML into named component slots](https://recursiveai.org/oneui/cookbook/pass-html-content-to-component-slots/)
- [Focus an input after a component update](https://recursiveai.org/oneui/cookbook/focus-input-after-component-update/)

## The short answer

Derive from `Component`, override `Build`, and use `Scope` to create nodes, wire events and insert the nodes into the component tree. The counter uses the same document API as a generated OneUI markup component, but there is no template or partial class. You can mount it in the reference document or use a UI host that applies OneUI change sets.

## How do you create UI elements programmatically in C#?

`scope.Element("button")` gives you the button’s node ID. `scope.Text(...)` gives you a separate text node. Insert the label into the button, then insert the button into `parent` using the `before` argument supplied to `Build`. Using that insertion point lets the caller decide where the component belongs among its siblings. Creating a node alone doesn’t place it anywhere.

## How do you update the UI when C# state changes?

A text node with a binding has a dependency mask. Here, `CountBit` is `1`, so the binding reads state slot `0`. The last argument to `SetState` is the index `0`, not the mask `1`. When the click changes that slot, the text binding runs again. Updating `_count` directly wouldn’t notify OneUI, and a binding that listens to the wrong bit won’t update when you expect. Use separate slots for values that can change independently, such as a count and an enabled flag.

If you use markup, the compiler tracks those dependencies for you. When you build the component in C#, keep the state indexes and the matching masks next to the fields they describe. See [the programmatic component API](https://recursiveai.org/oneui/docs/markup/#without-markup) for the other `Scope` methods available in `Build`.
