The CultureInfo
class supports a culture-sensitive processing of information. For example, when you format a date or a number, the CultureInfo
object determines whether to use periods or commas as separators, and whether to display the date before or after the month.
You can obtain a CultureInfo
object for a specific culture by using the static properties (like CultureInfo.CurrentCulture
, CultureInfo.InvariantCulture
, CultureInfo.GetCultureInfo(string name)
) or by creating a new instance of the class.
The following example demonstrates how to get the current culture and display its name and some formatting information:
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Get the current culture
CultureInfo currentCulture = CultureInfo.CurrentCulture;
Console.WriteLine($"Culture Name: {currentCulture.Name}");
Console.WriteLine($"English Name: {currentCulture.EnglishName}");
Console.WriteLine($"Native Name: {currentCulture.NativeName}");
// Example of date formatting
DateTime now = DateTime.Now;
Console.WriteLine($"Current Date ({currentCulture.Name}): {now.ToString("d")}");
// Example of number formatting
decimal price = 1234.56m;
Console.WriteLine($"Current Price ({currentCulture.Name}): {price.ToString("C")}");
}
}
public static CultureInfo GetCultureInfo(string name);
Retrieves a CultureInfo
object for the specified culture name.
public static CultureInfo GetCultureInfo(int culture);
Retrieves a CultureInfo
object for the specified culture identifier.
public override string ToString();
Returns the culture name.
public string Name { get; }
Gets the name of the culture, which is a standard 3-letter ISO 639-2 language code, preceded by an underscore and followed by a three-letter ISO 3166 country/region code.
public string EnglishName { get; }
Gets the full culture name in English.
public string NativeName { get; }
Gets the full culture name in the culture's own language.
public DateTimeFormatInfo DateTimeFormat { get; }
Gets a DateTimeFormatInfo
object that defines the culture-specific format of the current DateTime
object.
public NumberFormatInfo NumberFormat { get; }
Gets a NumberFormatInfo
object that defines the culture-specific format of the current Number
object.
public static CultureInfo CurrentCulture { get; set; }
Gets or sets the globalization information for the current thread.
public static CultureInfo InvariantCulture { get; }
Gets a CultureInfo
object that is culture-independent.