Подтвердить что ты не робот

Что-то похожее на IList.IndexOf(), но на IEnumerable <T>?

Есть ли какой-либо метод/метод расширения в IEnumerable, который позволяет мне найти индекс объекта объекта в нем? Как IndexOf() в IList?

indexPosition = myEnumerable.IndexOf() ?

Спасибо

4b9b3361

Ответ 1

An IEnumerable не является упорядоченным множеством.
Хотя большинство IEnumerables упорядочены, некоторые (например, Dictionary или HashSet) не являются.

Следовательно, LINQ не имеет метода IndexOf.

Однако вы можете написать один:

///<summary>Finds the index of the first item matching an expression in an enumerable.</summary>
///<param name="items">The enumerable to search.</param>
///<param name="predicate">The expression to test the items against.</param>
///<returns>The index of the first matching item, or -1 if no items match.</returns>
public static int FindIndex<T>(this IEnumerable<T> items, Func<T, bool> predicate) {
    if (items == null) throw new ArgumentNullException("items");
    if (predicate == null) throw new ArgumentNullException("predicate");

    int retVal = 0;
    foreach (var item in items) {
        if (predicate(item)) return retVal;
        retVal++;
    }
    return -1;
}
///<summary>Finds the index of the first occurence of an item in an enumerable.</summary>
///<param name="items">The enumerable to search.</param>
///<param name="item">The item to find.</param>
///<returns>The index of the first matching item, or -1 if the item was not found.</returns>
public static int IndexOf<T>(this IEnumerable<T> items, T item) { return items.FindIndex(i => EqualityComparer<T>.Default.Equals(item, i)); }

Ответ 3

Обратите внимание, что не может быть никакого метода экземпляра, потому что IEnumerable является ковариантным.

Все, что типа IEnumerable<string> должно реализовать IndexOf(string x), и, из-за ковариации, может быть отправлено на IEnumerable<object>.

Итак, теперь он предоставляет IndexOf(object x) то, что действительно IndexOf(string x), и поскольку не все objects являются strings, не могут работать для всех объектов.

IList может сделать это, потому что он инвариантен, т.е. вы не можете использовать IList<string> для IList<object>.