---
title: "Control YAML property order in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/order-yaml-properties/
description: "Control YAML property order in C# with OneYaml’s YamlPropertyOrder attribute. Put selected keys first without reordering the properties in your model."
version: "1.2.1"
---

# Control YAML property order 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/order-yaml-properties/order-yaml-properties.zip)

With `YamlPropertyOrder`, lower numbers come first. Properties with no order attribute default to `0`.

```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 job = new Job("daily", 3, "15 2 * * *");
Console.Write(YamlSerializer.Serialize(job));

record Job(
    [property: YamlPropertyOrder(-1)]
    string Name,
    int Retries,
    [property: YamlPropertyOrder(-2)]
    string Schedule);
```

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/order-yaml-properties/order-yaml-properties.zip) and `dotnet run`.

The snippet prints `schedule` first even though `Schedule` is declared last:

```yaml
schedule: 15 2 * * *
name: daily
retries: 3
```

## Variations

**Put a field at the end.** Replace the `Job` record with this version. `Schedule` has order `1`; the other two properties default to `0`, so `schedule` comes after both of them.

```csharp
record Job(string Name, int Retries,
    [property: YamlPropertyOrder(1)]
    string Schedule);
```

```yaml
name: daily
retries: 3
schedule: 15 2 * * *
```

## Related recipes

- [Serialize a C# object to YAML](https://recursiveai.org/oneyaml/csharp/cookbook/write-yaml/)
- [Change a YAML property name with an attribute](https://recursiveai.org/oneyaml/csharp/cookbook/rename-yaml-property-with-attribute/)

## The short answer

Use `[YamlPropertyOrder(n)]` on the members you want to move, then call `YamlSerializer.Serialize`. A member with no order attribute has order `0`, unless it has a `JsonPropertyOrder` attribute. In the example, `Schedule` (`-2`) comes before `Name` (`-1`) and both come before `Retries` (`0`). The numbers are sort keys, not output line numbers, so there’s no need to number every property or to use consecutive values.

## How do I change the order of properties when serializing YAML in C#?

For a class property, put the attribute immediately above its declaration. On a positional record, use `property:` as shown so the attribute applies to the generated property rather than the constructor parameter. Choose negative values for a short header you always want near the top and positive values for fields you want below the ordinary settings. In this example, ties keep declaration order: give both `Name` and `Schedule` order `-1` and `name` is written first.

The attribute changes the order in which the serializer writes siblings in the same YAML mapping. It doesn’t rename them: the example still emits camel-case keys. If the YAML key itself must change, that’s a separate decision made with `YamlPropertyName` or a naming policy. This is also a writing option, not a constraint on the order of keys in input. The [YAML specification](https://yaml.org/spec/1.2.2/#3221-mapping-key-order) treats mapping order as a serialization detail, so use it for readable output rather than to express which setting takes precedence.

## Can I use JsonPropertyOrder to set the YAML property order too?

Yes. OneYaml honors `System.Text.Json.Serialization.JsonPropertyOrder` when the property has no `YamlPropertyOrder`. If the model is written to both formats and the keys should appear in the same order, one JSON attribute is enough. If the YAML file needs a different order, use both attributes on that member: OneYaml uses the YAML attribute for its output. See the [serializer’s attribute table](https://recursiveai.org/oneyaml/csharp/docs/serializer/#attributes) for the other settings that work this way.
