问题描述
我有一些这样的代码:
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?
推荐答案
较新的 .Net Docs 现在有一个表格可以帮助您确定最适合您的情况.
来自 MSDN 的新建议在 Microsoft .NET 2.0 中使用字符串"
总结:以前使用 InvariantCulture 进行字符串比较、大小写和排序的代码所有者应该强烈考虑在 Microsoft .NET 2.0 中使用一组新的 String 重载.具体而言,设计为与文化无关和语言无关的数据应开始使用 StringComparison.Ordinal 或 StringComparison.OrdinalIgnoreCase 成员指定重载新的 StringComparison 枚举.这些强制执行类似于 strcmp 的逐字节比较,不仅避免了基本符号字符串的语言解释错误,而且提供了更好的性能.
Summary: Code owners previously using the
InvariantCulturefor string comparison, casing, and sorting should strongly consider using a new set ofStringoverloads in Microsoft .NET 2.0. Specifically, data that is designed to be culture-agnostic and linguistically irrelevant should begin specifying overloads using either theStringComparison.OrdinalorStringComparison.OrdinalIgnoreCasemembers of the newStringComparisonenumeration. These enforce a byte-by-byte comparison similar tostrcmpthat not only avoids bugs from linguistic interpretation of essentially symbolic strings, but provides better performance.
这篇关于通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)