StringComparer.CurrentCultureIgnoreCase 属性


获取一个 StringComparer 对象,该对象使用当前区域性的单词比较规则执行不区分大小写的字符串比较。Gets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture.

 static property StringComparer ^ CurrentCultureIgnoreCase { StringComparer ^ get(); };
public static StringComparer CurrentCultureIgnoreCase { get; }
member this.CurrentCultureIgnoreCase : StringComparer
Public Shared ReadOnly Property CurrentCultureIgnoreCase As StringComparer


用于字符串比较的新对象。A new object for string comparison.


当前区域性为与当前线程关联的 CultureInfo 对象;它由 CultureInfo.CurrentCulture 属性返回。The current culture is the CultureInfo object associated with the current thread; it is returned by the CultureInfo.CurrentCulture property.

当字符串与语言相关但其大小写不相关时,可以使用 CurrentCultureIgnoreCase 属性返回的 StringComparerThe StringComparer returned by the CurrentCultureIgnoreCase property can be used when strings are linguistically relevant but their case is not. 例如,如果向用户显示字符串,但大小写不重要,则应使用区分区域性的、不区分大小写的字符串比较来对字符串数据进行排序。For example, if strings are displayed to the user but case is unimportant, culture-sensitive, case-insensitive string comparison should be used to order the string data.


.NET Core 仅在 Linux 和 macOS 系统上运行: C 和 Posix 区域性的排序规则行为始终区分大小写,因为这些区域性不使用预期的 Unicode 排序规则顺序。.NET Core running on Linux and macOS systems only: The collation behavior for the C and Posix cultures is always case-sensitive because these cultures do not use the expected Unicode collation order. 建议使用除 C 或 Posix 以外的区域性执行区分区域性但不区分大小写的排序操作。We recommend that you use a culture other than C or Posix for performing culture-sensitive, case-insensitive sorting operations.

CurrentCultureIgnoreCase 属性实际上返回派生自 StringComparer 类的匿名类的实例。The CurrentCultureIgnoreCase property actually returns an instance of an anonymous class derived from the StringComparer class.

CurrentCultureIgnoreCase 属性 get 访问器的每个调用都将返回一个新的 StringComparer 对象,如下面的代码所示。Each call to the CurrentCultureIgnoreCase property get accessor returns a new StringComparer object, as the following code shows.

private void CompareCurrentCultureInsensitiveStringComparer()
   StringComparer stringComparer1, stringComparer2;
   stringComparer1 = StringComparer.CurrentCultureIgnoreCase;
   stringComparer2 = StringComparer.CurrentCultureIgnoreCase;
   // Displays false
Private Sub CompareCurrentCultureInsensitiveStringComparers()
   Dim stringComparer1, stringComparer2 As StringComparer
   stringComparer1 = StringComparer.CurrentCultureIgnoreCase
   stringComparer2 = StringComparer.CurrentCultureIgnoreCase
   ' Displays False
   Console.WriteLine(StringComparer.ReferenceEquals(stringComparer1, _
End Sub

若要提高性能,可以将 StringComparer 对象存储在局部变量中,而不是多次检索 CurrentCultureIgnoreCase 属性的值。To improve performance, you can store the StringComparer object in a local variable rather than retrieve the value of the CurrentCultureIgnoreCase property multiple times.