# Postcode lookup

`LookupAsync()` returns the addresses at a postcode, or the API's closest matching postcodes when it is not found. It never throws for an unknown postcode.

Lookup.cscsharp

```csharp
using IdealPostcodes;

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

var lookup = await client.LookupAsync("BR8 7RE");

if (lookup.Found)
{
    foreach (var address in lookup.Addresses)
    {
        Console.WriteLine($"{address.Line1}, {address.PostTown}, {address.Postcode}");
    }
}
else
{
    Console.WriteLine($"Did you mean: {string.Join(", ", lookup.Suggestions)}");
}
```

## Return value

A `PostcodeLookup`.

| Property      | Contents                                                                       |
| ------------- | ------------------------------------------------------------------------------ |
| `Found`       | `true` when the postcode exists                                                |
| `Addresses`   | The addresses on this page, as `AddressListItem` objects. Empty when not found |
| `Suggestions` | Up to five closest matching postcodes when not found, otherwise empty          |
| `Total`       | Addresses at the postcode across all pages. `0` when not found                 |

An unfound postcode costs no lookup. Other errors, such as an invalid key or an empty balance, still throw `ApiException`. See [error handling](/docs/sdks/dotnet/errors.md).

## PostcodesAsync()

`PostcodesAsync()` returns the full `PostcodeResponse`, with `Page`, `Limit` and `Total`, and throws `ApiException` with `Code` `4040` when the postcode is not found. Use it when you page through a large postcode or want the whole response.

Retrieve.cscsharp

```csharp
var response = await client.PostcodesAsync("BR8 7RE", new PostcodesParams { Page = 0 });

Console.WriteLine($"{response.Total} addresses, {response.Result.Count} on this page");
```

## Multiple Residence pagination

A small number of postcodes return more than 100 premises. The API returns 100 addresses per page, so pass `Page` until you have `Total` addresses.

Pages.cscsharp

```csharp
var addresses = new List<AddressListItem>();
var page = 0;
PostcodeResponse response;

do
{
    response = await client.PostcodesAsync("CV4 7AL", new PostcodesParams { Page = page++ });
    addresses.AddRange(response.Result);
}
while (addresses.Count < response.Total);
```

## Field selection

Set `Filter` to a comma separated list of the API's field names. Fields left out of the response come back as empty strings or `null` on the address object.

Filter.cscsharp

```csharp
var lookup = await client.LookupAsync("BR8 7RE", new PostcodesParams { Filter = "line_1,line_2,post_town,postcode" });
```

Every parameter is on the [`PostcodesAsync()`](/docs/sdks/dotnet/endpoints/postcodes.md) reference page.
