我有一个带有 HashSet 参数的方法,我需要在其中执行不区分大小写的包含:
public void DoSomething(HashSet<string> set, string item)
{
var x = set.Contains(item);
...
}
是否有办法使现有的 HashSet 不区分大小写(不要创建新的) ?
我正在寻找性能最好的解决方案。
剪辑
包含可以多次调用。因此,IEnumable 扩展对我来说是不可接受的,因为它的性能低于原生 HashSet Containsmethod。
解决方案
因为,我的问题的答案是否定的,这是不可能的,所以我创建并使用了以下方法:
public HashSet<string> EnsureCaseInsensitive(HashSet<string> set)
{
return set.Comparer == StringComparer.OrdinalIgnoreCase
? set
: new HashSet<string>(set, StringComparer.OrdinalIgnoreCase);
}