---
title: "Refresh a C# UI after mutating a list in place"
url: https://recursiveai.org/oneui/cookbook/refresh-component-after-mutating-list-in-place/
description: "Refresh a OneUI list after List<T>.Add without replacing the collection. Call the generated NotifyTasksChanged method and see the new HTML row."
version: "1.1.0"
---

# Refresh a C# UI after mutating a list in place

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in native windows on desktops and phones, 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/refresh-component-after-mutating-list-in-place/refresh-component-after-mutating-list-in-place.zip)

After editing `Tasks` in place, call the generated `NotifyTasksChanged()` method.

```shell
dotnet new console -n TaskDemo -f net10.0
cd TaskDemo
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.

```csharp
void AddReview()
{
    Tasks.Add(
        new(Tasks.Count + 1, "Review"));
    NotifyTasksChanged();
}
```

[The example](https://recursiveai.org/oneui/cookbook/refresh-component-after-mutating-list-in-place/refresh-component-after-mutating-list-in-place.zip) is the full program below: unzip it and `dotnet run`.

The handler goes in `TaskPanel.cs`, which defines the `[State]` list. The rendered list changes from `Draft` to `Draft, Review`.

## The full program

OneUI’s source generator pairs the `.html` file with the partial class of the same name. The example uses this package reference:

```xml
<PackageReference
    Include="RecursiveAI.OneUI"
    Version="1.1.0" />
```

`TaskPanel.html`:

```html
<button id="add" on:click="AddReview">
  Add review
</button>
<ul>
  <template each="{Tasks}" item="task"
            key="{task.Id}">
    <li>{task.Title}</li>
  </template>
</ul>
```

`TaskPanel.cs`:

```csharp
using OneUI.Components;

public partial class TaskPanel
{
    [State]
    public partial List<TaskRow> Tasks
    {
        get;
        set;
    }

    public TaskPanel()
        => Tasks = [new(1, "Draft")];

    void AddReview()
    {
        Tasks.Add(
            new(Tasks.Count + 1, "Review"));
        NotifyTasksChanged();
    }
}

public sealed record TaskRow(
    int Id, string Title);
```

`Demo.cs` uses a `ReferenceDocument`, an in-memory document that runs the component without a window. It prints the rendered rows before and after clicking Add review. If you created `TaskDemo` above, put the contents of `Demo.cs` in its `Program.cs` and save the other two files beside it.

To show a component in a real window, see [Show a dialog and toast in a OneApp desktop window](https://recursiveai.org/oneapp/cookbook/show-dialog-and-toast-with-ui-kit/).

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

var doc = new ReferenceDocument();
var ui = new UiContext(doc);
doc.Context = ui;
ui.Mount(new TaskPanel());

PrintTasks("Before click");
doc.Click("#add");
PrintTasks("After click");

void PrintTasks(string label)
{
    var tasks = doc.QueryAll("li")
        .Select(row => row.TextContent);
    var titles = string.Join(", ", tasks);
    Console.WriteLine($"{label}: {titles}");
}
```

`dotnet run` prints:

```text
Before click: Draft
After click: Draft, Review
```

If you omit `NotifyTasksChanged()`, the C# list still gets the item, but the click alone doesn’t tell the template to read it again.

## Variations

**Clear the list.** Replace the body of `AddReview` with these two lines and change the button label to Clear. The next update removes all rendered rows.

```csharp
Tasks.Clear();
NotifyTasksChanged();
```

**Assign a new list instead.** Replace the body of `AddReview` with the line below. The setter sees a different list, so there’s no separate notification call.

```csharp
Tasks = [
    ..Tasks, new(Tasks.Count + 1, "Review")
];
```

## Related recipes

- [Render a list in a C# HTML component template](https://recursiveai.org/oneui/cookbook/render-list-in-component-template/)
- [Conditionally render HTML in a C# component template](https://recursiveai.org/oneui/cookbook/render-conditional-content-in-template/)

## The short answer

Call `NotifyTasksChanged()` after adding, removing, or editing items in the existing `Tasks` list. OneUI generates `Notify<Name>Changed()` for a partial state property with that name. The notification tells OneUI to rerun bindings that read the property before the next commit. You don’t need to copy the collection just to update the view.

## Why doesn’t my C# UI update after adding an item to a list?

`Tasks.Add` changes the list object, not the value held by the `Tasks` property. It calls the property’s getter to obtain the list, then calls `Add` on that list; the setter never runs. Assigning `Tasks` back to itself isn’t a workaround, either: the generated setter checks equality before marking state changed. The explicit notification marks the property as changed without reassigning it. Use it after the last edit in a handler if you’re changing several items together. See [state and parameters](https://recursiveai.org/oneui/docs/markup/#state-and-parameters).

## How do you refresh the list after clearing it in C#?

Call `NotifyTasksChanged()` after `Tasks.Clear()` just as you would after `Tasks.Add`. The difference is what the template sees on its next pass: an empty list has no keys left, so it removes all the old rows. When you only append an item, the existing keys are still present, so OneUI keeps their rows and adds one. Each item needs a unique, stable key within the list; use the item ID rather than its current position if the rows can move.

If your application already produces a fresh collection, assign that collection through the generated property setter instead. If it shares a mutable list and needs to keep its identity, keep the list and notify after the edit. Both paths tell the template to read the current items; neither requires remounting the component.
