通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringCompar

0

本文介绍了通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一些这样的代码:

If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then
    DoSomething()
End If

我不关心这个案子.我应该使用 OrdinalIgnoreCaseInvariantCultureIgnoreCase 还是 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.OrdinalStringComparison.OrdinalIgnoreCase 成员指定重载新的 StringComparison 枚举.这些强制执行类似于 strcmp 的逐字节比较,不仅避免了基本符号字符串的语言解释错误,而且提供了更好的性能.

Summary: Code owners previously using the InvariantCulture for string comparison, casing, and sorting should strongly consider using a new set of String overloads in Microsoft .NET 2.0. Specifically, data that is designed to be culture-agnostic and linguistically irrelevant should begin specifying overloads using either the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase members of the new StringComparison enumeration. These enforce a byte-by-byte comparison similar to strcmp that not only avoids bugs from linguistic interpretation of essentially symbolic strings, but provides better performance.

这篇关于通常最好使用哪个 - StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

函数委托与函数
Func Delegate vs Function(函数委托与函数)...
2023-11-11 C#/.NET开发问题
6

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5