简体   繁体   English

我们不能在HashSet中使用StringComparison吗?

[英]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.

相关问题 如何使用StringComparison.InvariantCultureIgnoreCase - How can I use StringComparison.InvariantCultureIgnoreCase 我们可以缩短s1.Equals(s2,StringComparison.InvariantCultureIgnoreCase)吗? - Can we shorten s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase)? StringComparison,为什么“TH”不是以“T”开头 - StringComparison, why “TH” not start with “T” 为什么我不能预先分配一个哈希集<t></t> - Why can't I preallocate a hashset<T> 为什么我不能使用HashSet <string> 实现一个IEnumerable <string> 接口属性? - Why can't I use HashSet<string> to implement an IEnumerable<string> interface property? 我什么时候应该使用 HashSet<T> 类型? - When should I use the HashSet<T> type? 如何在 C# 中对字符串使用 StringComparison? - How to use StringComparison for strings in C#? 我什么时候应该使用 StringComparison.InvariantCulture 而不是 StringComparison.CurrentCulture 来测试字符串相等性? - When should I use StringComparison.InvariantCulture instead of StringComparison.CurrentCulture to test string equality? 为什么我不能写if(object是HashSet &lt;&gt;)但是如果我写的话就没关系(object.GetType()== typeof(HashSet &lt;&gt;)) - Why can't I write if (object is HashSet<>) but it's okay if I write (object.GetType() == typeof(HashSet<>)) 我如何使用HashSet <T> 作为字典键? - How do I use HashSet<T> as a dictionary key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM