Using LINQ for an object that only has GetEnumerator()

后端 未结 3 1351
萌比男神i
萌比男神i 2021-02-06 22:33

Can you use LINQ in an object that exposes only Add(), Remove(), Count(), Item() and GetEnumerator() from System.Collections.IEnumerator?

3条回答
  •  灰色年华
    2021-02-06 22:54

    The existing Linq extension methods work on objects that implement IEnumerable. I assume your object implements the non-generic IEnumerable interface. In that case you can use the Cast extension method to get a generic IEnumerable wrapper. For instance, if the elements are of type int :

    var wrapper = myObject.Cast();
    

    You can now use Linq on the wrapper

提交回复
热议问题