# Converters

Converters are used to convert one type to another type, sometimes with additional parameters. For example, a value can be converted to a string using a given format. The BindingSystem provides examples of implementing converters in the code, making it easy to understand and extend this functionality. There are essentially 2 types of Converters: <mark style="color:blue;">Safe</mark> and <mark style="color:orange;">Unsafe</mark>. The latter may fail during conversion and invalidate the entire data retrieval pipeline.

{% tabs %}
{% tab title="Minimal UI" %}

<figure><img src="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2FtjDW6qnZfcSHeW4xo9aM%2FConverter_MinimalUI_Simple.png?alt=media&#x26;token=28c418e6-3b72-4d45-81c4-4764a363cf7f" alt="" width="563"><figcaption><p>Inspector View with a Converter</p></figcaption></figure>
{% endtab %}

{% tab title="Verbose UI" %}

<figure><img src="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2F3YJLTBLqHLYILDmEzmLV%2FConverterExample.png?alt=media&#x26;token=80f09012-c0a6-4185-b0e3-7c394324ba2a" alt=""><figcaption><p>Inspector View with a Converter</p></figcaption></figure>
{% endtab %}
{% endtabs %}

To add a new converter it is sufficient to implement `IConverter<TFrom, TTo>` interface:

```csharp
public class MyConverter : IConverter<bool, string>
{
    public string Id => "My Converter";
    public string Description => "Transforms a bool into Valid or Invalid string";
    public bool IsSafe => true; // The conversion will always succeed
    public string Convert(bool value)
    {
        return value ? "Valid" : "Invalid";
    }
    public object Convert(object value) => Convert(value is bool bValue ? bValue : false);
}
```

Implement `IContextConverter` in addition to `IConverter<,>` if the converter needs to have information about bind source and path.

The system will automatically pick up the new converter and add it to converters list. To avoid auto registration of the new converter, add `[HideMember]` attribute over the class.

{% hint style="info" %}
Sometimes the system will need another recompilation round to successfully register the new converter.
{% endhint %}

{% hint style="warning" %}
The converter must have a parameterless constructor.
{% endhint %}

When having multiple converters, the system will give the user the possiblity to select which converter to use:

<div data-full-width="true"><figure><img src="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2F115AfgOWHaM6KT1VghHS%2FConverterExample_SelectConverter.png?alt=media&#x26;token=fa693d53-89be-4364-aca0-b84683928146" alt=""><figcaption></figcaption></figure> <figure><img src="https://2360058278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3rw9jjjjlPycieM3U3MU%2Fuploads%2FzjYf4QDK5x2heec947xx%2FConverterExample_SelectConverterPopup.png?alt=media&#x26;token=55aecd80-a71b-4e2b-a4d2-f8086dd7299c" alt=""><figcaption></figcaption></figure></div>
