# Address cleanse

`AddressCleanseAsync()` matches a freeform UK address to the closest address on file and scores the match. Use it to clean addresses you already hold, such as a CRM export.

Cleanse.cscsharp

```csharp
using IdealPostcodes;

using var client = new IdealPostcodesClient("ak_test");

var result = (await client.AddressCleanseAsync(new AddressCleanseBody("9 Malyons Rd, Swanley, BR8 7RE"))).Result;

if (result is GbrCleanseMatch match && match.Confidence > 0)
{
    Console.WriteLine($"{match.Match.Line1}, {match.Match.Postcode} ({match.Confidence})");
}
else
{
    Console.WriteLine("No match");
}
```

## Match or no match

Check the class and the score. A match is a `GbrCleanseMatch` with a `Confidence` above 0 and the matched `Address` in `Match`. A no-match is a `GbrCleanseNoMatch`, with every score at 0, no `Match` and every match level at `NO_MATCH`. `Query`, `Count`, `Fit`, `Confidence` and the match levels are on `ICleanseResponseResult`, so they read without a type check.

## Separate postcode and post town

If your records keep the postcode or post town in their own fields, set `Postcode` and `PostTown` on the body alongside the query rather than joining them into it. The second argument, `AddressCleanseParams`, takes the query parameters: `Tags`, `Context` and `RemoveOrganisation`.

CleanseFields.cscsharp

```csharp
var result = (await client.AddressCleanseAsync(new AddressCleanseBody("9 Malyons Rd") { Postcode = "BR8 7RE", PostTown = "Swanley" })).Result;
```

## Addresses outside the UK

Set `Context` to the country's ISO 3166-1 alpha-3 code. Cleanse defaults to the UK. The address datasets your key is licensed for decide which countries it can cleanse.

CleanseUsa.cscsharp

```csharp
var result = (await client.AddressCleanseAsync(
    new AddressCleanseBody("1600 Pennsylvania Avenue NW, Washington, DC 20500"),
    new AddressCleanseParams { Context = "usa" })).Result;
```

The result has the same shape for every country, so a match elsewhere is still a `GbrCleanseMatch` and the check above works unchanged.

## Organisation in address lines

Set `RemoveOrganisation` to `"true"`. The organisation and department names are dropped from the address lines and the remaining lines move up. Use it when you deliver to people rather than businesses.

CleanseLines.cscsharp

```csharp
var result = (await client.AddressCleanseAsync(
    new AddressCleanseBody("10 Downing Street, London, SW1A 2AA"),
    new AddressCleanseParams { RemoveOrganisation = "true" })).Result;

// ((GbrCleanseMatch)result).Match.Line1 is "10 Downing Street", not the organisation
```

`OrganisationName`, `DepartmentName` and the `Native` record are unchanged. If removing the names would leave no address lines, they are kept.

## Confidence threshold

The right threshold depends on your data. Each wrong, missing or misspelt element lowers the score, and one source tends to repeat the same mistakes. Cleanse a sample, sort it by `Confidence` and set your threshold where the matches start to go wrong.

The result also scores each element separately, in `OrganisationMatch`, `PremiseMatch`, `ThoroughfareMatch`, `LocalityMatch`, `PostTownMatch` and `PostcodeMatch`. The [API reference](/docs/api/address-cleanse.md) explains each level.

## Cost

One lookup for a match. A no-match costs nothing.

See [`AddressCleanseAsync()`](/docs/sdks/dotnet/endpoints/address-cleanse.md) for the body and parameters.
