它更快的名单,其中,T>卸下摆臂(T)或列表< T> .RemoveAt(int)方法?更快、名单、方法、列表

2023-09-03 03:37:32 作者:假言假语假温柔

它更快的List.Remove(T)或.NET集合List.RemoveAt(int)方法?这是一个速度值类型和引用类型不同?

Is it faster the List.Remove(T) or List.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;
}