我从 ReSharper 收到这条消息。 ReSharper 没有提出我认为在检查代码后合适的更改。因此,我担心问题可能是我不了解正在发生的事情,而不是 ReSharper 没有提供应有的帮助。
public interface IFrobable { }
public class DataClass
{
public List<IFrobable> Frobables {get; set;}
//...
}
public class WorkerClass
{
//...
void Frobinate(List<IFrobable> frobables)
{
//Frobs the input
}
void DoSomething(List<IFrobable> input>)
{
//Original code with Resharper on OfType<IActivity>
Frobinate(input.OfType<IFrobable>().ToList());
//Suggested change from ReSharper - Is this a generic refactor
//instead of issue specific?
Frobinate(Enumerable.OfType<IFrobable>(input).ToList());
//What I think should be safe to do - compiles and appears to work
Frobinate(input);
}
}
有什么理由可以说明我提出的更改可能不安全。
最佳答案
这是一个常规的函数调用:
Enumerable.OfType<IFrobable>(input)
这是相同的函数,但作为扩展方法调用:
input.OfType<IFrobable>()
在你的情况下:
Frobinate(input);
绝对没问题,因为:
input.OfType<IFrobable>().ToList()
等于:
input.Where(x => x as IFrobable != null).ToList()
在你的方法中input
已定义为 List<IFrobable>
那有什么意义呢?
关于c# - 冗余 'IEnumerable.OfType<T>' 调用考虑与 'null' 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848234/