When should I specify CurrentCulture or InvariantCulture and when should I leave it unspecified?

后端 未结 2 973
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 01:28

What is the best practice for specifying CurrentCulture or InvariantCulture and not specifying the culture at all?

From what I have read, if you\'re doing serializa

2条回答
  •  有刺的猬
    2020-12-31 01:36

    The default is already the current culture as initialized by Windows. So using CultureInfo.CurrentCulture explicitly is just a waste of time. Any decent serialization format (including binary serialization and XML serialization) will serialize a DateTime in a culture invariant way.

    Using a culture that is not the default is very dangerous. A thread will always be started with the default culture as specified by Windows and configured by the user when she installed Windows. .NET starts threadpool threads all the time and you'll risk getting a culture in that thread that is different from your main thread. Which can cause all manner of subtle problems. Like having a SortedList that suddenly isn't sorted anymore.

提交回复
热议问题