gpt4 book ai didi

c# - 从 LinkedList 快速删除项目

转载 作者:行者123 更新时间:2023-12-01 19:56:31 29 4
gpt4 key购买 nike

我有以下链接列表:

LinkedList<Segment> myList = new LinkedList<Segment>();

为什么我这样做:

myList.Remove(new Segment(4,8));

调用以下Segment.Equals()方法:

class Segment
{
...

public override bool Equals(object obj)
{
return Equals((Segment)obj);
}

}

而不是这个:

class Segment
{
...

public bool Equals(Segment other)
{
return other.V1 == V1 && other.V2 == V2;
}
}

有没有办法跳过object装箱和拆箱并使用后者(更快)的方法?

谢谢。

最佳答案

您需要元素类型来实现 IEquatable<T>接口(interface):

class Segment : IEquatable<Segment>
{
// ...

public bool Equals(Segment other)
{
return
(object)other != null &&
other.V1 == V1 &&
other.V2 == V2;
}
}

关于c# - 从 LinkedList<T> 快速删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12211831/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com