Description
Represents information about a culture, including the names of the culture, the calendar used, the formats for dates, currency, and numbers, and casing and sorting rules for strings. The CultureInfo
class is used to customize behavior that depends on the cultural context of the user, such as language, region, and the use of a particular encoding standard.
It plays a crucial role in internationalization (I18n) and localization (L10n) of applications. You can use CultureInfo
objects to get specific information about cultural settings or to set culture-specific properties for your application.
Syntax
Namespace: System.Globalization
Assembly: System.Runtime.dll
Fields
- InvariantCulture Provides culture-agnostic information for formatting and parsing numbers, currency, dates, and times.
- CurrentCulture Gets or sets the culture used by the current thread.
- CurrentUICulture Gets or sets the culture used by the ResourceManager to retrieve culture-specific resources.
Properties
- Name Gets the identifier for the culture.
- DisplayName Gets the display name for the culture.
- NativeName Gets the native name for the culture.
- LCID Gets the culture identifier (LCID) that is assigned by the operating system.
- KeyboardLayoutId Gets the identifier for the keyboard layout for the culture.
- DateTimeFormat Gets a DateTimeFormatInfo object that defines the formatting and parsing of dates and times.
- NumberFormat Gets a NumberFormatInfo object that defines the formatting and parsing of numeric values.
- TextInfo Gets a TextInfo object that defines the text casing and sorting rules.
Methods
- GetCulture(string name) Gets a read-only CultureInfo object based on the specified culture name.
- GetCultures(CultureTypes types) Returns a read-only copy of the array of cultures that are supported by the .NET Framework.
- Format(string format, object arg) Formats the value of the specified object into the string representation of the current culture.
- ToString() Returns the culture name.
Constructors
- CultureInfo(string name) Initializes a new instance of the CultureInfo class using the specified culture identifier.
- CultureInfo(string name, bool useUserOverride) Initializes a new instance of the CultureInfo class using the specified culture identifier and a value indicating whether to use the current user's culture settings.
Examples
Using CultureInfo to Format Dates
This example demonstrates how to use the CultureInfo
class to format a date according to different cultural conventions.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime now = DateTime.Now;
// Get the current culture
CultureInfo currentCulture = CultureInfo.CurrentCulture;
Console.WriteLine($"Current Culture: {currentCulture.DisplayName}");
Console.WriteLine($"Formatted Date (Current Culture): {now.ToString("d", currentCulture)}");
// Get a specific culture (e.g., English - United States)
CultureInfo usCulture = new CultureInfo("en-US");
Console.WriteLine($"\nCulture: {usCulture.DisplayName}");
Console.WriteLine($"Formatted Date (en-US): {now.ToString("d", usCulture)}");
// Get another specific culture (e.g., French - France)
CultureInfo frCulture = new CultureInfo("fr-FR");
Console.WriteLine($"\nCulture: {frCulture.DisplayName}");
Console.WriteLine($"Formatted Date (fr-FR): {now.ToString("d", frCulture)}");
// Using the invariant culture for consistent formatting
Console.WriteLine($"\nFormatted Date (Invariant Culture): {now.ToString("d", CultureInfo.InvariantCulture)}");
}
}
Setting Current Culture
This example shows how to temporarily change the current culture for specific operations.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
decimal price = 123.45m;
// Original culture
CultureInfo originalCulture = CultureInfo.CurrentCulture;
Console.WriteLine($"Original Culture: {originalCulture.Name}, Currency: {price.ToString("C")}");
// Change to German culture
CultureInfo deCulture = new CultureInfo("de-DE");
CultureInfo.CurrentCulture = deCulture;
Console.WriteLine($"New Culture: {CultureInfo.CurrentCulture.Name}, Currency: {price.ToString("C")}");
// Restore original culture
CultureInfo.CurrentCulture = originalCulture;
Console.WriteLine($"Restored Culture: {CultureInfo.CurrentCulture.Name}, Currency: {price.ToString("C")}");
}
}