What is the most elegant way to get a set of items by index from a collection?

后端 未结 14 2032
梦如初夏
梦如初夏 2020-12-13 04:57

Given

IList indexes;
ICollection collection;

What is the most elegant way to extract all T in

14条回答
  •  抹茶落季
    2020-12-13 05:40

    You could do it in an extension method:

    static IEnumerable Extract(this ICollection collection, IList indexes)
    {
       int index = 0;
       foreach(var item in collection)
       {
         if (indexes.Contains(index))
           yield item;
         index++;
       }
    }
    

提交回复
热议问题