.net - 通常最好使用 StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?

标签 .net vb.net localization

我有一些这样的代码:

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

我不关心这个案子。我应该使用 OrdinalIgnoreCase、InvariantCultureIgnoreCase 还是 CurrentCultureIgnoreCase?

最佳答案

<强> Newer .Net Docs now has a table to help you decide which is best to use in your situation.

来自 MSDN 的“New Recommendations for Using Strings in Microsoft .NET 2.0

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.

关于.net - 通常最好使用 StringComparison.OrdinalIgnoreCase 或 StringComparison.InvariantCultureIgnoreCase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72696/

相关文章:

c# - 用非法变量字符在C#中反序列化json

iPhone - 更改语言后本地化错误

localization - Flutter 中的本地化字符串

ios - UITest 本地化应用程序

c# - 增加 version.props 中的数字

c# - 基于链接改变文化 MVC4

c# - 代码 : TokenNotFound Message: User not found in token cache. 可能服务器已重新启动

javascript - UWP 应用中的 Web 请求

vb.net - 我可以将后台智能传输服务与 FTP 一起使用吗?

vb.net - 删除 NHibernate 表中的所有行