通常最好使用哪种-StringComparison.OrdinalIgnoreCase或StringComparison.InvariantCultureIgnoreCase? [英] Which is generally best to use -- StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?
问题描述
我有一些类似这样的代码:
I have some code like this:
If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then
DoSomething()
End If
我不在乎这个案子.我应该使用OrdinalIgnoreCase
,InvariantCultureIgnoreCase
还是CurrentCultureIgnoreCase
?
I don't care about the case. Should I use OrdinalIgnoreCase
, InvariantCultureIgnoreCase
, or CurrentCultureIgnoreCase
?
推荐答案
Newer .Net Docs now has a table to help you decide which is best to use in your situation.
来自MSDN的"新建议Microsoft .NET 2.0中使用字符串的方法"
摘要:以前使用
InvariantCulture
进行字符串比较,大小写和排序的代码所有者应强烈考虑在Microsoft .NET 2.0中使用一组新的String
重载. 具体来说,被设计为与文化无关且在语言上不相关的数据应该使用新的StringComparison
枚举的StringComparison.Ordinal
或StringComparison.OrdinalIgnoreCase
成员开始指定重载.它们强制执行类似于strcmp
的逐字节比较,不仅避免了实质上是符号字符串的语言解释带来的错误,而且还提供了更好的性能.
Summary: Code owners previously using the
InvariantCulture
for string comparison, casing, and sorting should strongly consider using a new set ofString
overloads in Microsoft .NET 2.0. Specifically, data that is designed to be culture-agnostic and linguistically irrelevant should begin specifying overloads using either theStringComparison.Ordinal
orStringComparison.OrdinalIgnoreCase
members of the newStringComparison
enumeration. These enforce a byte-by-byte comparison similar tostrcmp
that not only avoids bugs from linguistic interpretation of essentially symbolic strings, but provides better performance.
这篇关于通常最好使用哪种-StringComparison.OrdinalIgnoreCase或StringComparison.InvariantCultureIgnoreCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!