List T.Remove(T)或List T.RemoveAt(int)方法哪个更快? [英] Which is faster, the List<T>.Remove(T) or List<T>.RemoveAt(int) method?
本文介绍了List T.Remove(T)或List T.RemoveAt(int)方法哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
List< T> .Remove(T)
比 List< T> .RemoveAt(int)
.NET集合中的方法?值类型或引用类型的速度是否不同?
Is List<T>.Remove(T)
faster than the List<T>.RemoveAt(int)
method in .NET collections? Is speed different for value types or reference types?
推荐答案
List.Remove(T)在其索引中使用IndexOf和RemoveAt(int)实施。所以List.RemoveAt(int)更快。
List.Remove(T) uses IndexOf and RemoveAt(int) in its implementation. So List.RemoveAt(int) is faster.
public bool Remove(T item)
{
int index = this.IndexOf(item);
if (index >= 0)
{
this.RemoveAt(index);
return true;
}
return false;
}
这篇关于List T.Remove(T)或List T.RemoveAt(int)方法哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文