---
title: "Omit null values in C# YAML serialization"
url: https://recursiveai.org/oneyaml/csharp/cookbook/omit-null-yaml-values/
description: "Ignore null property values when serializing YAML in C#. Keep false, zero, and empty strings, or omit null on just one property."
version: "1.2.1"
---

# Omit null values in C# YAML serialization

**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/omit-null-yaml-values/omit-null-yaml-values.zip)

Leave out nulls, not `false` or `0`.

```shell
dotnet add package OneYaml
```

Targets [.NET 8 and .NET 10](https://dotnet.microsoft.com/download/dotnet).

```csharp
using OneYaml;
using static OneYaml.YamlIgnoreCondition;
var options = new YamlSerializerOptions
{ DefaultIgnoreCondition = WhenWritingNull };
var config = new Config(false, 0, null);
Console.Write(YamlSerializer.Serialize(
    config, options));
record Config(
    bool Enabled, int Retries, string? Note);
```

Prints the YAML without `note`:

```text
enabled: false
retries: 0
```

If you forget to pass `options` to `Serialize`, the default behavior writes `note: null` instead of omitting it.

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

## Variations

Omit null on `Note` only. Leave `Owner` explicit and keep the empty string in `Title`:

```csharp
using OneYaml;

Console.Write(YamlSerializer.Serialize(new Config(null, null, "")));

record Config(
    [property: YamlIgnore(Condition = YamlIgnoreCondition.WhenWritingNull)]
    string? Note,
    string? Owner,
    string Title);
```

Prints the YAML without `note`:

```yaml
owner: null
title: ''
```

## Related recipes

- [Serialize C# objects to YAML](https://recursiveai.org/oneyaml/csharp/cookbook/write-yaml/)
- [Deserialize YAML into a C# class](https://recursiveai.org/oneyaml/csharp/cookbook/parse-yaml-into-classes/)

---

## The short answer

Set `YamlSerializerOptions.DefaultIgnoreCondition` to `YamlIgnoreCondition.WhenWritingNull` and pass the options to `YamlSerializer.Serialize`. The resulting YAML has no key for a null member. The snippet at the top sets `Enabled` to `false` and `Retries` to `0`; both keys are still written.

## How do I omit null values without losing false or zero?

Use `WhenWritingNull`, not `WhenWritingDefault`, when `false` and `0` carry meaning. A retry count of zero might mean “do not retry,” while an omitted retry count might tell the program that reads the file to use its own setting. The two conditions are listed in the [serializer options](https://github.com/recursive-ai-oss/oneyaml-csharp/blob/7818048c4be434333767c47d66176fc626d1da8e/docs/serializer.md#L53-L83). `WhenWritingDefault` tests the default for the declared member type: `false` for `bool`, `0` for `int`, and null for `int?`.

This is a setting for writing C# object members to YAML. It does not go through an existing YAML file and remove its null keys. If you write several files with the same settings, reuse the options object; it becomes read-only after its first use.

## Is a missing YAML key the same as null or an empty string?

No. In the [YAML Core schema](https://yaml.org/spec/1.2.2/#103-core-schema), `note: null` has a present key and a null value. With `note: ''`, the key is present and its value is a string of length zero. If there’s no `note` key, the mapping has no entry for it at all. `WhenWritingNull` omits the first kind of value and leaves empty strings alone.

Check what reads the file before choosing between omission and explicit null. An application can interpret a missing setting as “inherit the default” and an explicit null as “clear this value.” If only some settings should disappear, use the [member attribute](https://github.com/recursive-ai-oss/oneyaml-csharp/blob/7818048c4be434333767c47d66176fc626d1da8e/docs/serializer.md#L98-L106) in the variation. On a positional record, `property:` puts it on the generated property. Without the global ignore setting, unmarked null members such as `Owner` still appear in the output.
