---
title: "Deduplicate email addresses with a stable key in C#"
url: https://recursiveai.org/onecontact/cookbook/create-stable-email-deduplication-key/
description: "Deduplicate email addresses in C# with a stable, versioned OneContact key. Compare aliases without changing the address you use for sending mail."
version: "1.1.0"
---

# Deduplicate email addresses with a stable key in C#

**OneContact** is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneContact) · [GitHub](https://github.com/recursive-ai-oss/onecontact-csharp) · [Docs](https://recursiveai.org/onecontact/docs/) · [Download example (.zip)](https://recursiveai.org/onecontact/cookbook/create-stable-email-deduplication-key/create-stable-email-deduplication-key.zip)

```shell
dotnet add package RecursiveAI.OneContact
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneContact;
var seen = new HashSet<string>();
foreach (var row in new[] {
    "Ada@example.org", "ada@EXAMPLE.ORG" })
    if (seen.Add(EmailAddress.Parse(row)
        .GetKey(KeyVersion.V1)))
        Console.WriteLine(row);
```

[The example](https://recursiveai.org/onecontact/cookbook/create-stable-email-deduplication-key/create-stable-email-deduplication-key.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Prints `Ada@example.org` once, keeping the first spelling.

## The full program

This example also shows provider-specific aliases, an unrelated domain with a plus tag, and an invalid row.

```csharp
using RecursiveAI.OneContact;

string[] rows =
[
    "Ada <Ada.Lovelace+news@gmail.com>",
    "adalovelace@googlemail.com",
    "ada+news@example.org",
    "ada@example.org",
    "not-an-address"
];
var seen = new HashSet<string>(StringComparer.Ordinal);
foreach (string row in rows)
{
    if (!EmailAddress.TryParse(row, out var email))
    {
        Console.WriteLine($"Invalid: {row}");
        continue;
    }

    string key = email.GetKey(KeyVersion.V1);
    string status = seen.Add(key) ? "Keep" : "Duplicate";
    Console.WriteLine($"{status}: {row} -> {key}");
}
```

`dotnet run` prints:

```text
Keep: Ada <Ada.Lovelace+news@gmail.com> -> adalovelace@gmail.com
Duplicate: adalovelace@googlemail.com -> adalovelace@gmail.com
Keep: ada+news@example.org -> ada+news@example.org
Keep: ada@example.org -> ada@example.org
Invalid: not-an-address
```

`Parse` throws `ContactParseException` on malformed input; use `TryParse` to set those rows aside without stopping the import.

## Variations

**Keep the comparison key separate from the sending address.** `Canonical` omits the display name but retains the local part for mail routing.

```csharp
using RecursiveAI.OneContact;

var email = EmailAddress.Parse("Ada <Ada@EXAMPLE.ORG>");
Console.WriteLine(email.Canonical);
Console.WriteLine(email.GetKey(KeyVersion.V1));
```

```text
Ada@example.org
ada@example.org
```

## Related recipes

- [Deduplicate contact records](https://recursiveai.org/onecontact/cookbook/deduplicate-contacts/)
- [Validate an email address](https://recursiveai.org/onecontact/cookbook/validate-email-address/)

## The short answer

Parse each mailbox with `EmailAddress`, take `GetKey(KeyVersion.V1)` and keep the first row for each distinct key. `HashSet.Add` returns `false` for a key you have already seen. Store the original or canonical address separately: the comparison key is not the address you should send to.

## Are email addresses case-sensitive when removing duplicates?

Case is not the same on both sides of `@`. [RFC 5321 §2.4](https://www.rfc-editor.org/rfc/rfc5321#section-2.4) says the domain is case-insensitive but the local part must be treated as case-sensitive for SMTP, even though relying on local-part case is discouraged. OneContact preserves that case in its canonical address but folds it in the comparison key. Two keys can therefore match without proving that two delivery addresses are interchangeable.

The domain in both forms is lowercase and uses the ASCII form for internationalized domains. The key also composes non-ASCII local parts to Unicode NFC and removes wrapping quotes when the unquoted text is a valid dot-atom, such as `"ada"` becoming `ada`. See the [V1 key rules](https://recursiveai.org/onecontact/docs/email/#key-keyversionv1) for the order of these steps.

## Should I strip plus tags before deduplicating email?

Not across every domain. V1 drops the first `+` and everything after it for `gmail.com`, `googlemail.com`, `outlook.com`, `hotmail.com`, `live.com`, and `fastmail.com`, provided the part before `@` isn’t left empty. The first two inputs in the full program match because V1 also removes dots for `gmail.com` and `googlemail.com` and maps both to `gmail.com`. The `example.org` rows keep distinct keys: OneContact has no provider rule for that domain.

A key match makes a useful review candidate when merging records with conflicting information. It does not prove that two rows belong to the same person, that a mailbox exists, or that both spellings accept mail. Keep the address the user supplied so you can decide what to do with the match.

## How do I prevent duplicate email entries in a database?

An in-memory set only covers one run. To catch duplicates across imports, persist the version and key with the record. If your application rejects duplicate keys, use a unique constraint on that pair with exact comparison rather than relying on a separate “does it exist?” read before inserting. If you retain duplicates for review, index the pair without making it unique.

Pinning `V1` matters for stored keys. OneContact documents that existing key versions keep their output; rule changes use a new version. Don’t mix keys from different versions in one comparison without rebuilding them from the retained addresses. The key itself contains mailbox text, not an anonymized identifier.
