Rider/ReSharper 给了我可能的多重枚举警告:
public void ProcessProductCodes(IEnumerable<string> productCodes) {
if (productCodes.Any()) {
DoStuff(productCodes);
}
}
Run Code Online (Sandbox Code Playgroud)
这是误报,还是 Any() 函数确实搞乱了集合的枚举?
该IEnumerable接口表示可以迭代的项目序列,但不对序列的起源做出任何假设。例如,它可能是数据库查询。如果是这种情况,您将在这里对数据库进行 2 次调用,一次用于检查序列中是否有任何项目,另一次将它们传递给函数DoStuff,这显然不是最佳性能,并且 ReSharper 发出警告你关于这件事。
为了避免此问题,您有两种不同的选择。如果项目集合已在内存中,您可以通过将函数的签名更改为:
public void ProcessProductCodes(ICollection<string> productCodes) { ... }
Run Code Online (Sandbox Code Playgroud)
如果您不能保证这一点,您可以在函数的开头执行.ToList()or操作:.ToArray
public void ProcessProductCodes(IEnumerable<string> productCodes) {
var productCodesList = productCodes.ToList();
if (productCodesList .Any()) {
DoStuff(productCodesList );
}
Run Code Online (Sandbox Code Playgroud)
ReSharper 会为您完成此操作,只需选择快速重构(通常使用Alt+Enter)。