November 25, 2010 Leave a comment
When persisting a date as a string, it is a good idea to use the invariant culture to format it—this ensures that a consistent format is always used internally, regardless of the culture used for presenting dates to the user.
string dateToSave = DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture);
In addition, the InvariantCulture is useful for storing data that will not be displayed directly to end users. Storing data in a culture-independent format guarantees a known format that does not change.
For example, screen coordinates are generally serialized as a comma-delimited list (x, y). If these coordinates are not serialized/de-serialized using an invariant culture, depending on the current UI culture, the list separator could end up being a semi-colon and as a result will not be valid as screen coordinates and will cause a system exception.
Just remember, when storing formatted numbers or dates in files, using InvariantCulture is the best option.