---
title: "Change YAML indentation in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/set-yaml-indentation/
description: "Change YAML indentation in C# with OneYaml’s IndentSize option. Compare the default two spaces with four spaces in the same nested data."
version: "1.2.1"
---

# Change YAML indentation 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/set-yaml-indentation/set-yaml-indentation.zip)

Set the number of spaces OneYaml adds at each level when it writes YAML.

```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;
var config = new {
    Web = new { Port = 8080 }
};
var options = new YamlSerializerOptions
    { IndentSize = 4 };
Console.Write(YamlSerializer.Serialize(
    config, options));
```

Unzip [the example](https://recursiveai.org/oneyaml/csharp/cookbook/set-yaml-indentation/set-yaml-indentation.zip), then `dotnet run`.

Prints `port` indented by four spaces under `web`:

```yaml
web:
    port: 8080
```

`Web` and `Port` become `web` and `port` because OneYaml writes property names in camelCase by default (see [`PropertyNamingPolicy`](https://recursiveai.org/oneyaml/csharp/docs/serializer/#options)).

## The full program

The download writes the same configuration twice, with a list nested under `routes` so you can see the indentation at two levels.

```csharp
using OneYaml;

var config = new
{
    Web = new
    {
        Port = 8080,
        Routes = new[] { "/", "/health" }
    }
};

Console.WriteLine("Default (2 spaces):");
Console.Write(YamlSerializer.Serialize(config));

var options = new YamlSerializerOptions { IndentSize = 4 };
Console.WriteLine("Custom (4 spaces):");
Console.Write(YamlSerializer.Serialize(config, options));
```

```text
Default (2 spaces):
web:
  port: 8080
  routes:
    - /
    - /health
Custom (4 spaces):
web:
    port: 8080
    routes:
        - /
        - /health
```

A value outside 1–8 throws `ArgumentOutOfRangeException` when you create the options, before serialization starts.

## Variations

**Keep the default.** Replace the options setup and write call in the snippet at the top with:

```csharp
Console.Write(YamlSerializer.Serialize(config));
```

```yaml
web:
  port: 8080
```

## Related recipes

- [Write YAML from C#](https://recursiveai.org/oneyaml/csharp/cookbook/write-yaml/)
- [Edit YAML and keep comments](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-keep-comments/)

## The short answer

Set `YamlSerializerOptions.IndentSize` to the number of spaces to add at each level, then pass the options to `YamlSerializer.Serialize`. [OneYaml’s serializer options](https://recursiveai.org/oneyaml/csharp/docs/serializer/#options) allow 1 through 8; the default is 2. This is an output setting. You don’t need to match it when you read the result.

## How should indentation be used in YAML files?

Indentation groups keys and lists. In the example, `port` and `routes` start in the same column because both belong directly to `web`. The list markers start farther in because they belong to `routes`. If you move `port` all the way left, it becomes a top-level key alongside `web`: that’s a change to the data, not just its appearance.

The [YAML 1.2.2 indentation rules](https://yaml.org/spec/1.2.2/#61-indentation-spaces) require siblings in a block to share an indentation level and let deeper indentation mark children. Use spaces, not tabs, for indentation. That restriction is on the whitespace that marks structure, not on every tab character that might appear inside a string value.

## How many spaces should you indent YAML: two or four?

Both work for the configuration above. Match the convention of the files you already generate. With two spaces per level, the list markers in the example are four spaces in from the left; with four per level they’re eight spaces in. Setting `IndentSize = 4` does not add four spaces to every line: top-level keys still start at the left margin, and each level adds four more spaces.

The option affects new output from the serializer. If your input is an existing YAML file, serializing the data again doesn’t preserve the original whitespace or comments. For a targeted change that should leave the surrounding text alone, use [OneYaml’s document editing API](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-keep-comments/) rather than treating reserialization as a formatting-only edit.
