[英]Can't we use StringComparison in HashSet?
C# 3.5: I can't get this code to compile: C#3.5:我无法编译此代码:
HashSet<string> classDeclarations = new HashSet<string>(StringComparison.InvariantCultureIgnoreCase);
I get this error: 我收到此错误:
Argument 1: cannot convert from 'System.StringComparison' to 'System.Collections.Generic.IEqualityComparer' 参数1:无法从“ System.StringComparison”转换为“ System.Collections.Generic.IEqualityComparer”
So I can't pass a comparison? 所以我无法通过比较?
StringComparison
is an enum used by many string-related functions. StringComparison
是许多与字符串相关的函数使用的枚举。
Since HashSet<T>
has nothing to do with strings, it wouldn't make sense for it to take a StringComparison
. 由于HashSet<T>
与字符串无关,因此采用StringComparison
毫无意义。
You need to use the static properties of the StringComparer
class, which implement IEqualityComparer<String>
. 您需要使用实现IEqualityComparer<String>
的StringComparer
类的静态属性。
您需要这样做
HashSet<string> myHashSet = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.