如何在C#中实现List< T> .IndexOf()? [英] How is List<T>.IndexOf() implemented in C#?
本文介绍了如何在C#中实现List< T> .IndexOf()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在考虑调用List<T>.Indexof(item)
的性能.我不确定顺序算法是O(n)的性能还是二叉树是O(log(n))的性能?
I was thinking about the performance of calling List<T>.Indexof(item)
. I am not sure if it will be a O(n) performance for a sequential algorithm or O(log(n)) performance for a binary tree??
推荐答案
使用Reflector for .NET,我们可以看到:
Using Reflector for .NET we can see:
public int IndexOf(T item)
{
return Array.IndexOf<T>(this._items, item, 0, this._size);
}
public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
{
return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count);
}
internal virtual int IndexOf(T[] array, T value, int startIndex, int count)
{
int num = startIndex + count;
for (int i = startIndex; i < num; i++)
{
if (this.Equals(array[i], value))
return i;
}
return -1;
}
这篇关于如何在C#中实现List< T> .IndexOf()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文