System.Globalization.CultureInfo.InvariantCulture 属性

本文提供此 API 參考文件的補充備註。

不因文化特性而異;它與英文相關聯,但不與任何國家/地區相關聯。 您可以在呼叫 CultureInfo 具現化方法時,使用空字串 (“”) 來指定不因名稱而異的文化特性。 這個屬性 CultureInfo.InvariantCulture也會擷取不因文化特性而異的實例。 它幾乎可以在命名空間中 System.Globalization 需要文化特性的任何方法中使用。 、和 NumberFormatCompareInfoDateTimeFormat屬性所傳回的物件也會反映不因文化特性而異的字串比較和格式化慣例。

不同於使用者自定義或 .NET Framework 或作業系統更新可能會變更文化特性敏感數據,隨著時間推移和安裝的文化特性,不因文化特性而異,且無法由使用者自定義。 這讓不因文化特性而異的作業特別有用,例如格式化和剖析保存格式化數據的作業,或排序和排序作業,而不論文化特性為何,都需要以固定順序顯示數據的作業。

字串作業

您可以使用不因文化特性而異的字串作業,這些作業不受目前文化特性慣例影響,且文化特性之間一致。 例如,您可能想要以固定順序顯示已排序的數據,或將一組標準大小寫慣例套用至字串,而不論目前的文化特性為何。 若要這樣做,請將 物件傳遞 InvariantCulture 至具有 CultureInfo 參數的方法,例如 Compare(String, String, Boolean, CultureInfo)ToUpper(CultureInfo)

保存數據

InvariantCulture屬性可用來以與文化特性無關的格式保存數據。 這提供不會變更的已知格式,可用來串行化和還原串行化跨文化特性的數據。 還原串行化數據之後,就可以根據目前使用者的文化慣例適當地格式化數據。

例如,如果您選擇以字串形式保存日期和時間數據,您可以將 對象傳遞至 DateTime.ToString(String, IFormatProvider) 或 方法以建立字串,而您可以將 對象傳遞InvariantCultureInvariantCultureDateTime.Parse(String, IFormatProvider)DateTimeOffset.ToString(IFormatProvider)DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 方法,將字串轉換回日期和時間值。 這項技術可確保當不同文化特性的使用者讀取或寫入數據時,基礎日期和時間值不會變更。

下列範例會使用不因文化特性而異,將值保存 DateTime 為字串。 然後,它會剖析字串,並使用法文(法國)和德國(德國)文化特性的格式慣例來顯示其值。

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

安全性決策

如果您要根據字串比較或案例變更的結果,做出安全性決策(例如是否允許存取系統資源),則不應該使用不因文化特性而異。 相反地,您應該呼叫包含 參數並提供 StringComparison.OrdinalStringComparison.OrdinalIgnoreCase 做為自變數的方法StringComparison,以執行區分大小寫或不區分大小寫的序數比較。 執行區分文化特性字串作業的程式代碼,如果目前的文化特性已變更,或者執行程式碼之電腦上的文化特性與用來測試程式代碼的文化特性不同,可能會導致安全性弱點。 相反地,序數比較只取決於比較字元的二進位值。