Skip to content
Tutorials Center
My learning

Topics

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.

C#
using System.Globalization;

string stored = 1234.5.ToString(CultureInfo.InvariantCulture);   // "1234.5"
double back = double.Parse(stored, CultureInfo.InvariantCulture);

Try it

C# Current and invariant culture

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.

C# Changing the culture of a thread

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

C#
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. InvariantCulture is 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) and double.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.CurrentCulture inside an async method flows to its continuations. DefaultThreadCurrentCulture sets the starting value for threads that do not choose one.
  • This site's runner is invariant. The examples run with invariant globalization: CurrentCulture is the invariant culture (its name is the empty string), and GetCultureInfo("de-DE") throws CultureNotFoundException, 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 (InvariantGlobalization in 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() and string.Compare use the current culture unless you pick ToUpperInvariant() or a StringComparison. The Turkish dotless i is the classic example of a culture changing a result.
  • Neutral and specific cultures. fr is neutral (language only) and cannot format currency meaningfully; fr-CA and fr-FR are 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.