C# CultureInfo Tutorial: Basics & Examples
The CultureInfo class, located in the System.Globalization namespace, is used to represent specific cultural information such as language, country/region, and date formats. It can be utilized to achieve internationalization and localization.
Here are some common examples of using the CultureInfo class:
- Create a CultureInfo object:
- Create a new instance of CultureInfo with the specified culture set to “zh-CN”.
- Obtain the default cultural information of the current system:
- Set the currentCulture variable to the value of the current culture.
- Format date and time using the CultureInfo object:
- Create a variable called ‘date’ and set it to the current date and time. Then convert this date to a string in the “d” format using the specified culture information and store it in a variable called ‘formattedDate’.
- Format currency using the CultureInfo object.
- Convert the decimal amount of 1234.56 to a string in the specified culture format.
- Obtain culture-specific resources using the CultureInfo object.
- Retrieve the day of the week in the current language and assign it to the variable “greeting”.
Using the CultureInfo class makes it easy to handle dates, times, currencies, and other information in different cultural backgrounds, enabling the development of globalized applications.