System.Globalization.Properties
The System.Globalization
namespace provides classes that define culture-sensitive information, such as the conventions for date and time formatting, currency formatting, string comparisons, and the layout of the characters in the writing system.
Properties
This section details the key properties available within the System.Globalization
namespace or commonly used when working with globalization concepts.
CultureInfo.CurrentCulture
Gets or sets the CultureInfo
object that represents the culture used by the current thread. This property influences the formatting of dates, times, numbers, and currency values, as well as string comparisons.
public static CultureInfo CurrentCulture { get; set; }
CultureInfo.CurrentUICulture
Gets or sets the CultureInfo
object that represents the culture used by the Resource Manager to look up culture-specific resources. This property affects the language of user interface elements and other localized resources.
public static CultureInfo CurrentUICulture { get; set; }
RegionInfo.CurrentRegion
Gets a RegionInfo
object that provides information about the current culture's region. This includes currency symbols, number formats specific to the region, and geographical details.
public static RegionInfo CurrentRegion { get; }
DateTimeFormatInfo.CurrentInfo
Gets a DateTimeFormatInfo
object that defines the patterns and culture-sensitive representations of dates and times for the current CultureInfo.CurrentCulture
. It provides access to short date patterns, long date patterns, time separators, AM/PM designators, and more.
public static DateTimeFormatInfo CurrentInfo { get; }
NumberFormatInfo.CurrentInfo
Gets a NumberFormatInfo
object that defines the culture-sensitive patterns and representations of numbers for the current CultureInfo.CurrentCulture
. This includes currency symbols, decimal separators, group separators, and negative sign representations.
public static NumberFormatInfo CurrentInfo { get; }
TextInfo.CurrentCulture
Gets a TextInfo
object that defines the case-mapping behavior and text manipulation rules for the current CultureInfo.CurrentCulture
. This is crucial for accurate string comparisons and case conversions across different languages.
public TextInfo CurrentCulture { get; }
Commonly Used Properties within CultureInfo
When you have a CultureInfo
object, you can access numerous properties that define its specific characteristics:
CultureAwareComparer.StringComparer
Provides a static property returning a StringComparer
that uses the current culture for comparisons.
public static StringComparer StringComparer { get; }
CultureInfo.Name
Gets the name of the culture, including the language and subculture. For example, "en-US" for English (United States) or "fr-FR" for French (France).
public string Name { get; }
CultureInfo.DisplayName
Gets a friendly, displayable name for the culture. For example, "English (United States)".
public string DisplayName { get; }
CultureInfo.IsNeutralCulture
Indicates whether the culture is a neutral culture (i.e., it does not have a specific region associated with it, like "en" for English).
public bool IsNeutralCulture { get; }
CultureInfo.Parent
Gets the parent culture of the current culture. For example, the parent of "en-US" is "en".
public CultureInfo Parent { get; }
Important: Understanding and correctly setting the CurrentCulture
and CurrentUICulture
are fundamental for building applications that handle internationalization and localization effectively. Changes to these properties can impact the behavior of many .NET Framework methods.
Example Usage
Here's a simple example demonstrating how to access and use some of these properties:
using System;
using System.Globalization;
public class GlobalizationExample
{
public static void Main(string[] args)
{
// Get current culture information
CultureInfo englishCulture = CultureInfo.CurrentCulture;
Console.WriteLine($"Current Culture: {englishCulture.DisplayName}");
Console.WriteLine($"Language: {englishCulture.Name}");
// Format a number using current culture
double price = 1234.56;
Console.WriteLine($"Formatted Price: {price.ToString("C", englishCulture)}");
// Change culture to French (France) for demonstration
try
{
CultureInfo frenchCulture = new CultureInfo("fr-FR");
CultureInfo.CurrentCulture = frenchCulture;
Console.WriteLine($"\nSwitched Culture to: {CultureInfo.CurrentCulture.DisplayName}");
Console.WriteLine($"Formatted Price in FR: {price.ToString("C", CultureInfo.CurrentCulture)}");
}
catch (CultureNotFoundException)
{
Console.WriteLine("\nFrench (France) culture not found on this system.");
}
}
}