c# - 冗余 'IEnumerable.OfType<T>' 调用考虑与 'null' 进行比较

标签 c# resharper

我从 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/

相关文章:

c# - 即使选择单个测试,Nunit 也会在 TestFixture 上运行所有测试

c# - 调试编辑后,Visual Studio 将有效代码误认为无效

c# - 在单击另一个控件之前,DataGridView 复选框不会更新

c# - onClick 绑定(bind)到 Url.ActionLink 仅在第一次有效

c# - 如何使用 linq 仅对一列进行分组

c# - ReSharper C# Intellisense 在键入 Override 关键字后显示可覆盖的成员

c# - 自动列出所有方法参数以将它们向前传递

c# - 什么是 Visual Studio 08 C# 程序集信息 GUID 设置?

c# - TreeNode 上 IEnumerator 的参数

c# - 单元测试中的进度指示