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

后端 未结 14 2058
梦如初夏
梦如初夏 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:54

    As I understand it, an ICollection may not necessarily have any order which is why there isn't an extremely elegant solution to access things by index. You many want to consider using a dictionary or list to store the data in the collection.

    The best way I can think of is to iterate through the collection while keeping track of what index you are on. Then check if the indexes list contains that index. If so, return that element.

提交回复
热议问题