# Responses

Every method returns a class built from the API's JSON. Fields are PascalCase properties: `line_1` is `Line1` and `post_town` is `PostTown`.

Fields.cscsharp

```csharp
var response = await client.ResolveAddressAsync("paf_2670849");
var address = response.Result;

Console.WriteLine($"{address.Line1}, {address.PostTown}, {address.Postcode}");
```

## Types

All in the `IdealPostcodes` namespace.

| Kind                             | Examples                                                                                                                                |
| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| One class per operation response | `PostcodeResponse`, `AutocompleteResponse`. Each has `Code`, `Message` and `Result`                                                     |
| Everything inside a result       | `Address`, `AddressListItem`, `UkAddressSuggestion`, `Licensee`, `Config` and the native records                                        |
| Request classes                  | `<Operation>Params` for query parameters and `<Operation>Body` for request bodies, such as `FindAddressParams` and `AddressCleanseBody` |

Nullability follows the API: a field the API can leave out or send as `null` is nullable, so the compiler shows where to check.

## Fields with more than one shape

Some results come in one of several forms, and the SDK picks the class that matches. The property is an interface; match on the class.

| Field                                                               | Classes                                                                                   |
| ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `FindAddressAsync()` hits, `IAutocompleteResultHit`                 | `UkAddressSuggestion` with a `Udprn`, or `AddressSuggestion` for addresses outside the UK |
| `AddressCleanseAsync()` result, `ICleanseResponseResult`            | `GbrCleanseMatch` with a `Match` address, or `GbrCleanseNoMatch`                          |
| `EmailValidationAsync()` result, `IEmailResponseResult`             | `Email`, or `UnknownEmail` when deliverability cannot be checked                          |
| `PhoneNumberValidationAsync()` result, `IPhoneNumberResponseResult` | `PhoneNumber`, or `InvalidPhoneNumber`                                                    |

Fields every form has, such as `Id` and `Suggestion` on a hit or `Confidence` on a cleanse result, are on the interface and read without a match.

Union.cscsharp

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

if (result is GbrCleanseMatch match)
{
    Console.WriteLine($"{match.Match.Postcode} with confidence {match.Confidence}");
}
```

## Values of more than one type

A few fields can arrive as more than one JSON type, such as a number or a string. They are `Scalar`, which keeps the value as sent. Read it with `AsString()`, `TryGetInt64()`, `TryGetDouble()` or `TryGetBoolean()`, and check `Kind` for the JSON type. In a request body, a `Scalar` takes a number, string or boolean directly, and `Scalar.Null` sends `null`.

Scalar.cscsharp

```csharp
var key = (await client.KeyAvailabilityAsync("ak_test")).Result;

Console.WriteLine(key.Context.AsString());
```

## New API values

Your integration keeps working when the API adds something.

* **Fields with a fixed set of values**, such as `PremiseMatch` on a cleanse result, are `string`, with the known values in the XML docs. A value added after your SDK version arrives as it was sent, so compare against the values you handle and fall through for the rest.
* **New fields** land in `AdditionalProperties` on the object that holds them, as `JsonElement`s, and are written back if you serialize the object.
* **A new form of a result**, such as a native record for a new dataset, arrives as an `Unknown…` class that keeps the raw JSON in `Value`.

A field the API sends with a different shape from the one this SDK version expects does not fail the response either. A number sent as a string, or a string sent as a number, is converted. Any other wrong value, such as a fraction where a whole number belongs, reads as `null`, or `0` or `false` for a property that cannot be null, and a list the API leaves out, sends as `null` or sends as something else comes back empty. Only a body that is not JSON at all throws `UnserializableResponseException`, with the raw `Body`. See [errors](/docs/sdks/dotnet/errors.md).

## Native record

`Native` on an `Address` is the raw record from the address's source dataset, as a class named for the dataset: `PafAddress` for Royal Mail PAF, `AbpAddress` for AddressBase Premium, `UspsAddress` for the United States and so on. The `Dataset` property on the address says which. A dataset the SDK does not know yet comes back as `UnknownNativeRecord`, so a new dataset never breaks your integration. `Native` is `null` when the API sends no record.

Native.cscsharp

```csharp
var address = (await client.ResolveAddressAsync("paf_2670849")).Result;

switch (address.Native)
{
    case PafAddress paf:
        Console.WriteLine(paf.DeliveryPointSuffix);
        break;
    case UnknownNativeRecord unknown:
        Console.WriteLine(unknown.Value.GetProperty("dataset"));
        break;
}
```

The [data sources](/docs/data.md) pages document every dataset's fields.

## JSON

Every response class serializes back to the API's JSON with `System.Text.Json`, field names and unknown fields included. `KeyLogsAsync()` is the one operation that returns text: a CSV string.
