C# Reference ยท Strings and Text
The CultureInfo class
Reference for CultureInfo in C#: current and invariant culture, how culture changes number, date and string behavior, and how to avoid culture bugs.
System.Globalization.CultureInfo describes the conventions of a language and region: how to write numbers and dates, which characters separate decimals and thousands, how to sort and compare text, and which calendar to use. Formatting and parsing methods such as ToString, double.Parse and string.Format consult a culture whenever you do not pass one.
Most culture bugs come from that silent default. A program that works on your machine formats 1234.5 as 1,234.5, and the same program in another country writes 1.234,5 or fails to parse the text it just wrote. For data that machines read (files, URLs, JSON, logs) pass CultureInfo.InvariantCulture. For text a person reads, use the person's culture.
using System.Globalization; string stored = 1234.5.ToString(CultureInfo.InvariantCulture); // "1234.5" double back = double.Parse(stored, CultureInfo.InvariantCulture);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class CultureInfo : IFormatProvider { public static CultureInfo CurrentCulture { get; set; } public static CultureInfo CurrentUICulture { get; set; } public static CultureInfo InvariantCulture { get; } public static CultureInfo DefaultThreadCurrentCulture { get; set; } public CultureInfo(string name); public static CultureInfo GetCultureInfo(string name); public static CultureInfo CreateSpecificCulture(string name); public string Name { get; } public string DisplayName { get; } public NumberFormatInfo NumberFormat { get; set; } public DateTimeFormatInfo DateTimeFormat { get; set; } public CompareInfo CompareInfo { get; } public TextInfo TextInfo { get; } }
| Name | Description |
|---|---|
name |
An IETF language tag such as en-US, fr-FR or ja-JP. The empty string means the invariant culture. |
CurrentCulture |
Drives formatting and parsing (numbers, dates, currency) for the current thread or async flow. |
CurrentUICulture |
Drives which resource files (translated strings) are loaded. It does not affect number formatting. |
DefaultThreadCurrentCulture |
The culture given to new threads in the app domain. Set it once at startup in apps that serve many threads. |
NumberFormat, DateTimeFormat |
The detailed rules: separators, symbols, month names, patterns. |
Returns: GetCultureInfo returns a cached, read-only instance. new CultureInfo(name) returns a fresh, modifiable one. CreateSpecificCulture("en") picks a specific culture for a neutral one.
Errors / undefined behavior: an unknown name throws CultureNotFoundException. Assigning null to CurrentCulture throws ArgumentNullException. Changing a read-only instance (InvariantCulture, anything from GetCultureInfo) throws InvalidOperationException.
Usage notes
- Invariant is not English.
InvariantCultureis a stable, region-free culture with fixed rules:.as decimal separator,,for groups, month/day/year dates, and a generic currency sign. It never changes between operating systems or updates, which is why it is the right choice for persisted and protocol data. - Pass the culture explicitly.
value.ToString(CultureInfo.InvariantCulture)anddouble.Parse(text, CultureInfo.InvariantCulture)document the intent and cannot be broken by a user's regional settings. Analyzer rule CA1305 flags the calls that forget. - Current culture is per thread (really per async flow). Setting
CultureInfo.CurrentCultureinside anasyncmethod flows to its continuations.DefaultThreadCurrentCulturesets the starting value for threads that do not choose one. - This site's runner is invariant. The examples run with invariant globalization:
CurrentCultureis the invariant culture (its name is the empty string), andGetCultureInfo("de-DE")throwsCultureNotFoundException, which is why the second example catches it. Run culture-specific code on your own machine to see real German, French or Japanese output. - Invariant globalization mode. Container images and trimmed apps often enable it (
InvariantGlobalizationin the project file) to skip the ICU library. Culture-sensitive comparison and casing then behave like ordinal or invariant rules, which can surprise code written for ICU. - Culture also affects strings.
ToUpper(),ToLower()andstring.Compareuse the current culture unless you pickToUpperInvariant()or aStringComparison. The Turkish dotless i is the classic example of a culture changing a result. - Neutral and specific cultures.
fris neutral (language only) and cannot format currency meaningfully;fr-CAandfr-FRare specific. Prefer specific cultures for formatting. - Identity values. Never use culture-formatted text as a dictionary key or file name that other machines must read.