CultureInfo.InvariantCulture CultureInfo.InvariantCulture CultureInfo.InvariantCulture CultureInfo.InvariantCulture Property

定義

取得與文化特性無關的 (不變的) CultureInfo 物件。Gets the CultureInfo object that is culture-independent (invariant).

public:
 static property System::Globalization::CultureInfo ^ InvariantCulture { System::Globalization::CultureInfo ^ get(); };
public static System.Globalization.CultureInfo InvariantCulture { get; }
member this.InvariantCulture : System.Globalization.CultureInfo
Public Shared ReadOnly Property InvariantCulture As CultureInfo

屬性值

與文化特性無關的 (不變的) 物件。The object that is culture-independent (invariant).

備註

而異的文化特性是不區分文化特性;相關聯的英文語言使用,但不是能搭配任何國家/地區。The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. 依名稱指定文化特性而異使用空字串 ("") 的呼叫中CultureInfo具現化方法。You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.InvariantCulture 也會擷取執行個體的文化特性而異。CultureInfo.InvariantCulture also retrieves an instance of the invariant culture. 幾乎任何方法中用於System.Globalization需要文化特性的命名空間。It can be used in almost any method in the System.Globalization namespace that requires a culture. 這類屬性所傳回的物件CompareInfoDateTimeFormat,和NumberFormat也反映字串比較和格式設定慣例的文化特性而異。The objects returned by properties such as CompareInfo, DateTimeFormat, and NumberFormat also reflect the string comparison and formatting conventions of the invariant culture.

不同於區分文化特性的資料,也就是有可能變更的使用者自訂或.NET Framework 或作業系統更新,而異的文化特性資料穩定一段時間後跨已安裝的文化特性,並且無法由使用者自訂。Unlike culture-sensitive data, which is subject to change by user customization or by updates to the .NET Framework or the operating system, invariant culture data is stable over time and across installed cultures and cannot be customized by users. 這可讓而異的文化特性特別適用於作業需要文化特性無關的結果,例如格式化和剖析作業保存格式化的資料,或排序及順序要求中,顯示資料的作業不論文化特性的固定的順序。This makes the invariant culture particularly useful for operations that require culture-independent results, such as formatting and parsing operations that persist formatted data, or sorting and ordering operations that require that data be displayed in a fixed order regardless of culture.

字串作業String Operations

您可以使用文化特性而異的文化特性字串作業,不會受到目前執行緒文化特性的慣例,以及屬於一致跨文化特性。You can use the invariant culture for culture-sensitive string operations that are not affected by the conventions of the current thread culture and that are consistent across cultures. 例如,您可以依照固定順序顯示,或套用一組標準的大小寫慣例,不論目前執行緒文化特性的字串排序的資料。For example, you may want sorted data to appear in a fixed order or apply a standard set of casing conventions to strings regardless of the current thread culture. 若要這樣做,您傳遞InvariantCulture物件的方法有CultureInfo參數,例如Compare(String, String, Boolean, CultureInfo)ToUpper(CultureInfo)To do this, you pass the InvariantCulture object to a method that has a CultureInfo parameter, such as Compare(String, String, Boolean, CultureInfo) and ToUpper(CultureInfo).

保存資料Persisting Data

InvariantCulture屬性可以用來保存資料的文化特性無關的格式。The InvariantCulture property can be used to persist data in a culture-independent format. 這會提供已知的格式,不會變更,並可用來序列化和還原序列化跨文化特性的資料。This provides a known format that does not change and that can be used to serialize and deserialize data across cultures. 將資料還原序列化之後,它可以根據目前使用者的文化特性的慣例,適當地進行格式化。After the data is deserialized, it can be formatted appropriately based on the cultural conventions of the current user.

比方說,如果您選擇要保存字串形式的日期和時間資料,您可以傳遞InvariantCulture物件DateTime.ToString(String, IFormatProvider)或是DateTimeOffset.ToString(IFormatProvider)方法來建立字串,而且您可以傳遞InvariantCulture物件DateTime.Parse(String, IFormatProvider)DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles)將字串轉換成日期和時間值的方法。For example, if you choose to persist date and time data in string form, you can pass the InvariantCulture object to the DateTime.ToString(String, IFormatProvider) or DateTimeOffset.ToString(IFormatProvider) method to create the string, and you can pass the InvariantCulture object to the DateTime.Parse(String, IFormatProvider) or DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) method to convert the string back to a date and time value. 這項技術可確保基礎的日期和時間值不會變更讀取或寫入使用者從不同的文化特性的資料時。This technique ensures that the underlying date and time values do not change when the data is read or written by users from different cultures.

下列範例會使用區分文化特性來保存DateTime做為字串的值。The following example uses the invariant culture to persist a DateTime value as a string. 然後它會剖析字串,並顯示其值,使用法文 (法國) 和德文 (德國) 文化特性的格式化慣例。It then parses the string and displays its value by using the formatting conventions of the French (France) and German (Germany) cultures.

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

public class Example 
{
   public static void Main() 
   {
      // Persist the date and time data.
      StreamWriter sw = new StreamWriter(@".\DateData.dat");
      
      // Create a DateTime value.      
      DateTime dtIn = DateTime.Now;
      // Retrieve a CultureInfo object.
      CultureInfo invC = CultureInfo.InvariantCulture;
      
      // Convert the date to a string and write it to a file.
      sw.WriteLine(dtIn.ToString("r", invC));
      sw.Close();

      // Restore the date and time data.
      StreamReader sr = new StreamReader(@".\DateData.dat");
      String input;
      while ((input = sr.ReadLine()) != null) 
      {
         Console.WriteLine("Stored data: {0}\n" , input);    

         // Parse the stored string.
         DateTime dtOut = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);

         // Create a French (France) CultureInfo object.
         CultureInfo frFr = new CultureInfo("fr-FR");
         // Displays the date formatted for the "fr-FR" culture.
         Console.WriteLine("Date formatted for the {0} culture: {1}" , 
                           frFr.Name, dtOut.ToString("f", frFr));

         // Creates a German (Germany) CultureInfo object.
         CultureInfo deDe= new CultureInfo("de-De");
         // Displays the date formatted for the "de-DE" culture.
         Console.WriteLine("Date formatted for {0} culture: {1}" , 
                           deDe.Name, dtOut.ToString("f", deDe));
      }
      sr.Close();
   }
}
// The example displays the following output:
//    Stored data: Tue, 15 May 2012 16:34:16 GMT
//    
//    Date formatted for the fr-FR culture: mardi 15 mai 2012 16:34
//    Date formatted for de-DE culture: Dienstag, 15. Mai 2012 16:34
Imports System.Globalization
Imports System.IO

Module Example
   Public Sub Main()
      ' Persist the date and time data.
      Dim sw As New StreamWriter(".\DateData.dat")
      
      ' Create a DateTime value.      
      Dim dtIn As DateTime = DateTime.Now
      ' Retrieve a CultureInfo object.
      Dim invC As CultureInfo = CultureInfo.InvariantCulture
      
      ' Convert the date to a string and write it to a file.
      sw.WriteLine(dtIn.ToString("r", invC))
      sw.Close()

      ' Restore the date and time data.
      Dim sr As New StreamReader(".\DateData.dat")
      Dim input As String = String.Empty
      Do While sr.Peek() >= 0 
         input = sr.ReadLine()
         Console.WriteLine("Stored data: {0}" , input)    
         Console.WriteLine()
         
         ' Parse the stored string.
         Dim dtOut As DateTime = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind)

         ' Create a French (France) CultureInfo object.
         Dim frFr As New CultureInfo("fr-FR")
         ' Displays the date formatted for the "fr-FR" culture.
         Console.WriteLine("Date formatted for the {0} culture: {1}" , 
                           frFr.Name, dtOut.ToString("f", frFr))

         ' Creates a German (Germany) CultureInfo object.
         Dim deDe As New CultureInfo("de-De")
         ' Displays the date formatted for the "de-DE" culture.
         Console.WriteLine("Date formatted for {0} culture: {1}" , 
                           deDe.Name, dtOut.ToString("f", deDe))
      Loop
      sr.Close()
   End Sub
End Module
' The example displays the following output:
'    Stored data: Tue, 15 May 2012 16:34:16 GMT
'    
'    Date formatted for the fr-FR culture: mardi 15 mai 2012 16:34
'    Date formatted for de-DE culture: Dienstag, 15. Mai 2012 16:34

安全性決策Security Decisions

如果您要進行安全性決策 (例如是否要允許系統資源的存取權) 根據字串比較或大小寫變更的結果,您不應該使用文化特性而異。If you are making a security decision (such as whether to allow access to a system resource) based on the result of a string comparison or a case change, you should not use the invariant culture. 相反地,您應該執行區分大小寫或不區分大小寫的序數比較,藉由呼叫的方法,包括StringComparison參數,並提供其中一個StringComparison.OrdinalStringComparison.OrdinalIgnoreCase做為引數。Instead, you should perform a case-sensitive or case-insensitive ordinal comparison by calling a method that includes a StringComparison parameter and supplying either StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase as an argument. 如果目前的文化特性變更,或正在執行的程式碼的電腦上的文化特性不同於用來測試程式碼的文化特性,則會執行區分文化特性字串作業的程式碼可能會造成安全性弱點。Code that performs culture-sensitive string operations can cause security vulnerabilities if the current culture is changed or if the culture on the computer that is running the code differs from the culture that is used to test the code. 相反地,序數比較取決於比較字元的二進位值。In contrast, an ordinal comparison depends solely on the binary value of the compared characters.

適用於

另請參閱