问题描述
什么是这两个类之间的区别?
What is the difference between these two classes ?
我用 System.StringComparer.OrdinalIgnoreCase()
和 System.StringComparison.OrdinalIgnoreCase()
键,都产生相同的结果。难道他们有什么区别或他们都一样的吗?
I have used System.StringComparer.OrdinalIgnoreCase()
and System.StringComparison.OrdinalIgnoreCase()
and both yield the same results. Do they have any difference or they both same ?
推荐答案
StringComparer
是一个类
它实现了比较接口,如的IComparer
,的IEqualityComparer
, 的IComparer<弦乐方式>
,因此它可以用来比较两个字符串
StringComparer
is a Class
which implements comparison interfaces like IComparer
, IEqualityComparer
, IComparer<String>
, so that it can be used to compare two strings.
StringComparison
是一个简单的枚举
,您可以在通过某些方法来选择你所想要哪种比较。在这种情况下,我怀疑下面的方法将使用 StringComparer
来进行实际比较。
StringComparison
is simply an Enum
that you can pass in to certain methods to indicate which kind of comparison you want. In that case I suspect the underlying method would use a StringComparer
to do the actual comparison.
当使用每个
字符串的具体方法类似的中只接受 StringComparison
价值,所以这就是你会在这种情况下使用什么。
String specific methods like String.Equals only accepts a StringComparison
value, so that's what you would use in this case.
您会使用 StringComparer
为此采取了比较器作为一个参数,被称为在上下文字符串的地方进行比较的方法。例如,如果你有一个列表与LT;弦乐>
,想的在不区分大小写的方式,列表中,你可以做:
You would use a StringComparer
for methods which take a comparer as a parameter, and are called in a context where strings will be compared. For example, if you had a List<String>
, and wanted to Sort that list in a case-insensitive way, you could do:
myList.Sort(StringComparer.OrdinalIgnoreCase);
这篇关于类System.StringComparer和System.StringComparison之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!