Which is generally best to use -- StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?(通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?)
问题描述
我有一些这样的代码:
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
