共用方式為


System.Globalization.CultureInfo.InvariantCulture 属性

備註

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

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

不同於會因使用者自定義或 .NET Framework 或作業系統更新而改變的文化特性敏感數據,定文化數據在時間經過和跨文化安裝時是穩定的,且無法由使用者自定義。 這讓不變的文化特別有用,特別是在需要文化獨立結果的場合,例如持續保存格式化數據的格式化和剖析操作,或是不論文化特性為何都需以固定的順序顯示數據的排序和安排操作。

字串作業

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

持久化數據

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

例如,如果您選擇以字串形式保存日期和時間數據,您可以將 對象傳遞至 InvariantCulture 或 方法以建立字串,而您可以將 對象傳遞DateTime.ToString(String, IFormatProvider)DateTimeOffset.ToString(IFormatProvider)InvariantCultureDateTime.Parse(String, 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: {input}\n");

         // 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 {frFr.Name} culture: {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 {deDe.Name} culture: {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參數的方法,並提供StringComparison.OrdinalStringComparison.OrdinalIgnoreCase做為參數,以執行區分大小寫或不區分大小寫的序數比較。 執行區分文化特性字串作業的程式代碼,如果目前的文化特性已變更,或者執行程式碼之電腦上的文化特性與用來測試程式代碼的文化特性不同,可能會導致安全性弱點。 相反地,序數比較只取決於比較字元的二進位值。