在陣列中執行不區分文化特性的字串作業Performing Culture-Insensitive String Operations in Arrays

Array.SortArray.BinarySearch 方法的多載預設會使用 Thread.CurrentCulture 屬性執行區分文化特性的排序。Overloads of the Array.Sort and Array.BinarySearch methods perform culture-sensitive sorts by default using the Thread.CurrentCulture property. 這些方法所傳回的區分文化特性的結果可能會因為文化特性排序順序上的差異而有所不同。Culture-sensitive results returned by these methods can vary by culture due to differences in sort orders. 若要消除區分文化特性的行為,請使用此方法中可接受 comparer 參數的其中一個多載。To eliminate culture-sensitive behavior, use one of the overloads of this method that accepts a comparer parameter. comparer 參數會指定要在比較陣列中元素時使用的 IComparer 實作。The comparer parameter specifies the IComparer implementation to use when comparing elements in the array. 為參數指定一個使用 CultureInfo.InvariantCulture 的自訂非變異值比較子類別。For the parameter, specify a custom invariant comparer class that uses CultureInfo.InvariantCulture. 如需自訂非變異值比較子類別的範例,請參閱在集合中執行不區分文化特性的字串作業主題中的「使用 SortedList 類別」子主題。An example of a custom invariant comparer class is provided in the "Using the SortedList Class" subtopic of the Performing Culture-Insensitive String Operations in Collections topic.

注意:CultureInfo.InvariantCulture 傳遞給比較方法的確會執行不區分文化特性的比較。Note Passing CultureInfo.InvariantCulture to a comparison method does perform a culture-insensitive comparison. 不過,它不會讓某些項目進行非語言比較,例如檔案路徑、登錄機碼和環境變數。However, it does not cause a non-linguistic comparison, for example, for file paths, registry keys, and environment variables. 它也不支援根據比較結果所做出的安全性決策。Neither does it support security decisions based on the comparison result. 若要進行非語言比較或需要支援根據結果的安全性決策,應用程式應該使用可接受 StringComparison 值的比較方法。For a non-linguistic comparison or support for result-based security decisions, the application should use a comparison method that accepts a StringComparison value. 接著,應用程式應該會傳遞 OrdinalThe application should then pass Ordinal.

另請參閱See also