Export (0) Print
Expand All

Using the InvariantCulture Property 

The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region. You can use InvariantCulture in almost any method in the System.Globalization namespace that requires a culture. However, you should use the invariant culture only for processes that require culture-independent results, such as formatting and parsing data that is persisted to a file. In other cases, it produces results that might be linguistically incorrect or culturally inappropriate.

Security Considerations

If a security decision will be made based on the result of a string comparison or case change, use an ordinal comparison that ignores case instead of using InvariantCulture. This is because the default implementations of methods such as String.Compare, String.ToUpper, and String.ToLower use the CultureInfo.CurrentCulture property. Code that performs culture-sensitive string operations can cause security vulnerabilities if CultureInfo.CurrentCulture is changed or if the culture on the computer on which the code runs differs from the culture the developer used to test the code. The behavior expected by the developer writing a string operation will differ from the code's actual behavior on the computer on which the operation runs. In contrast, an ordinal comparison depends solely on the binary value of the compared characters.

String Operations

If you want to perform a culture-sensitive string operation that is not affected by the value of CultureInfo.CurrentCulture, use a method that accepts a CultureInfo parameter, specifying the value of the CultureInfo.InvariantCulture property for the CultureInfo parameter. Use InvariantCulture with methods such as String.Compare, String.ToUpper, and String.ToLower to eliminate cultural variations and ensure consistent results. For more information about using the InvariantCulture property to perform culture-insensitive string operations, see Culture-Insensitive String Operations.

Persisting Data

The InvariantCulture is useful for storing data that will not be displayed directly to end users. Storing data in a culture-independent format guarantees a known format that does not change. When users from different cultures access the data, it can be formatted appropriately based on the user. For example, if you store DateTime types in a text file, formatted for the InvariantCulture, use the InvariantCulture property when you call the DateTime.ToString method to store the strings and the Date.Parse method to retrieve the strings. This will ensure that the underlying values of the DateTime types do not change when the data is read or written by users from different cultures.

The following code example demonstrates how to initialize CultureInfo with the invariant culture by using an empty string ("") or CultureInfo.InvariantCulture.

// The following lines are equivalent.
CultureInfo Invc = New CultureInfo("");
CultureInfo Invc = CultureInfo.InvariantCulture;

The following code example illustrates writing a DateTime to a file as a string formatted for the InvariantCulture using the DateTime.ToString method. The string is then read from the file in the InvariantCulture format and parsed to a DateTime using the Date.Parse method. The DateTime is then formatted and displayed for the cultures "fr-FR" and "ja-JP".

using System;
using System.IO;
using System.Globalization;

public class TextToFile 
{
   private const string FILE_NAME = "MyDateFile.txt";
   public static void Main(String[] args) 
   {
      if (File.Exists(FILE_NAME)) 
      {
         Console.WriteLine("{0} already exists!", FILE_NAME);
         return;
      }
      StreamWriter sw = File.CreateText(FILE_NAME);
      
      // Creates a DateTime.      
      DateTime dtIn = DateTime.Now;
      // Creates a CultureInfo set to InvariantCulture.
      CultureInfo InvC = new CultureInfo("");
      // Converts dt to a string formatted for InvariantCulture,
      // and writes it to a file.
      sw.WriteLine (dtIn.ToString("d",InvC));
      sw.Close();

      if (!File.Exists(FILE_NAME)) 
      {
         Console.WriteLine("{0} does not exist!", FILE_NAME);
         return;
      }
      StreamReader sr = File.OpenText(FILE_NAME);
      String date;
      while ((date=sr.ReadLine())!=null) 
      {
         Console.WriteLine("\nThe date stored in the file formatted for 
               the invariant culture is:\n{0}" , date);    

         // Parses the string stored in the file,
         // and stores it in a DateTime.
         DateTime dtout = DateTime.Parse(date, InvC);

         // Creates a CultureInfo set to "fr-FR".
         CultureInfo frc = new CultureInfo("fr-FR");
         // Displays the date formatted for the "fr-FR" culture.
         Console.WriteLine("\nThe date read from the file and formatted 
               for the culture \"fr-FR\" is:\n{0}" , dtout.ToString("d", 
               frc));

         // Creates a CultureInfo set to "ja-JP".
         CultureInfo jpn= new CultureInfo("ja-JP");
         // Displays the date formatted for the "ja-JP" culture.
         Console.WriteLine("\nThe date read from the file and formatted 
               for the culture \"ja-JP\" is:\n{0}" , dtout.ToString("d", 
               jpn));
      }
      Console.WriteLine ("\nThe end of the stream has been reached.");
      sr.Close();
   }
}

This code produces the following output:

The date stored in the file formatted for the invariant culture is:
07/24/2001

The date read from the file and formatted for the culture "fr-FR" is:
24/07/2001

The date read from the file and formatted for the culture "ja-JP" is:
2001/07/24

The end of the stream has been reached.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft