How does OfType() Work?

后端 未结 2 1652
逝去的感伤
逝去的感伤 2020-12-19 05:32

How does OfType() Work?

I read this link about what\'s going on but how exactly does the LINQ provider know how to get all objects matching the specified type. I kno

相关标签:
2条回答
  • 2020-12-19 06:01

    Your current implementation -- by design -- doesn't support value-types.

    If you wanted something closer to LINQ's OfType method, that supports all types, then try this:

    public IEnumerable<TResult> OfType<TResult>(IEnumerable source)
    {
        foreach (object item in source)
        {
            if (item is TResult)
                yield return (TResult)item;
        }
    }
    
    0 讨论(0)
  • 2020-12-19 06:08

    It looks like a good implementation to me, but it looks kind of implementation specific (you are referring to this.InnerList). If you created an extension method (that's supported in 2.0 is it not?) that extends IEnumerable, you would be able to use it on any enumerable collection, would you not?

    0 讨论(0)
提交回复
热议问题