---
title: "Replace a YAML alias without changing its anchor in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/replace-yaml-alias-with-local-value/
description: "Use YamlDocument.Set in C# to replace a YAML alias with a local mapping while leaving the shared anchor and its other aliases unchanged."
version: "1.2.1"
---

# Replace a YAML alias without changing its anchor in C#

**OneYaml** is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/replace-yaml-alias-with-local-value/replace-yaml-alias-with-local-value.zip)

Call `doc.Set` on the key that holds the alias (`/web`), not on a key inside it (`/web/size`).

```shell
dotnet add package OneYaml
```

The library targets .NET 8 and later; the example project uses [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).

```csharp
using OneYaml;

using var doc = YamlDocument.Parse("""
    base: &pool {size: 2}
    web: *pool
    jobs: *pool
    """);
doc.Set("/web", new { size = 4 });
Console.WriteLine(doc);
```

Setting `/web/size` instead of `/web` on the original document throws a `YamlException` with category `RefusedAlias`: it would write through the alias.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/oneyaml/csharp/cookbook/replace-yaml-alias-with-local-value/replace-yaml-alias-with-local-value.zip) and `dotnet run`.

The snippet prints:

```yaml
base: &pool {size: 2}
web:
  size: 4
jobs: *pool
```

## Variations

**Change the shared default on purpose.** Replace the `Set` call in the example with this one. Both aliases stay in the YAML and now read the updated size.

```csharp
doc.Set("/base/size", 6);
```

```yaml
base: &pool {size: 6}
web: *pool
jobs: *pool
```

## Related recipes

- [Read YAML anchors and merge keys in C#](https://recursiveai.org/oneyaml/csharp/cookbook/anchors-aliases-merge-keys/)
- [Edit YAML without losing comments in C#](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-keep-comments/)

## The short answer

Call `YamlDocument.Set` on the path that contains the alias itself. Here it’s `/web`, not `/web/size`. The new object is written as a YAML mapping, and the other alias still points to the original mapping. Supply every key that `web` should have: this is a replacement, not a merge.

## How do YAML anchors and aliases share values?

An anchor such as `&pool` names a value in a YAML document. An alias such as `*pool` refers to that earlier value; it isn’t a separate editable copy. In the input above, `web` and `jobs` refer to the same one-key mapping. OneYaml follows aliases when reading with `Get`, so reading `/web/size` before the edit gives `2`. But writing `/web/size` while `web` is still an alias would have to change the shared mapping. OneYaml refuses that edit; see the [anchor and alias editing guide](https://recursiveai.org/oneyaml/csharp/docs/editing/#anchors-and-aliases).

That leaves two explicit choices. To change every consumer, edit the value where the anchor is defined, as the variation does. To change only one consumer, replace the alias. After the replacement, `web` is a mapping in its own right, so subsequent writes to `/web/size` no longer go through an alias. A later edit to `/base/size` won’t affect `web` either.

## Can I override just one key in a YAML alias?

Not with a bare alias: `web: *pool` stands for the entire value. Replacing it with an explicit mapping works even when the YAML reader has no merge-key support, but you must include any settings you still want from the original. That’s useful when you want a complete local configuration that won’t change with the defaults.

If `web` should still inherit other keys, use a mapping with a `<<` merge and a local key instead, provided the tool that reads the file supports merges. In a reader that supports the [YAML merge-key extension](https://yaml.org/type/merge.html), a key written locally wins over one inherited from the anchor. That’s a different behavior: later edits to shared keys that aren’t overridden still affect `web`.
