StringFormat.GenericDefault and StringFormat.GenericTypographic

Beware StringFormat.GenericDefault and StringFormat.GenericTypographic. While multiple calls to one or other property yield new .NET StringFormat instances, under the hood they’ll all refer to the same blob of unmanaged memory as a StringFormat’s really a wrapper around some GDI+ functions.

This means that you shouldn’t be using either property as a basis for a custom StringFormat without first making a call to .Clone. If you do modify either default StringFormat then the change is with you and, crucially, anything else using either property in the same app domain.

The alternative, and one that’s probably better anyway, is to new up a StringFormat with your settings fully-specified without leaning on the defaults. Just remember to Dispose it when you’re done.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.