String.Format and IFormatProvider


Have you ever noticed this overload of String.Format(IFormatProvider, String, Object[])? IFormatProvider is actually a very useful interface that allows us to create custom formats to transform our inputs. The IFormatProvider link has examples on how to do it.

This is my version on how to create a custom formatter factory that understand custom formats we create, and routes to the appropriate formatter. If a non-custom format is used, the conversion will still be performed as normal. In the example, I’m trying to create custom formats “bsb” and “acctno” which will convert numbers to a “999-999” format and “1234-56789” format using the CustomFormatProvider.

Read the rest of this entry »

Advertisements