代码检查:可能无意中在集合中进行了线性查找
此检查会报告在集合类型如 HashSet<T> 或 ISet<T> 上带有显式比较器调用 Contains 的情况。 该调用会通过 LINQ,将快速的集合查找变为线性查找。
示例
using System;
using System.Collections.Generic;
using System.Linq;
var set = new HashSet<string>();
Console.WriteLine(set.Contains("value", StringComparer.OrdinalIgnoreCase));
using System;
using System.Collections.Generic;
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Console.WriteLine(set.Contains("value"));
如何修复它
没有针对此检查的专用快速修复。 典型的修复方法是使用所需的比较器创建集合,或者在集合已有正确比较器时使用普通的实例 Contains 调用。
2026年 5月 8日