通常最好使用哪种-StringComparison.OrdinalIgnoreCase或StringComparison.InvariantCultureIgnoreCase? [英] Which is generally best to use -- StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?

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

问题描述

我有一些类似这样的代码:

I have some code like this:

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?

推荐答案

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.OrdinalStringComparison.OrdinalIgnoreCase成员开始指定重载.它们强制执行类似于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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆