C# 如何使用LinkedList<;T>;。Find()方法,其中T是用户定义的数据类型(对象)

C# 如何使用LinkedList<;T>;。Find()方法,其中T是用户定义的数据类型(对象),c#,linked-list,C#,Linked List,C#中LinkedList的Find()方法适用于字符串等,但如何将其用于结构、对象等 这是我的密码: { LinkedList<Item> TestLinkedList = new LinkedList<Item>(); TestLinkedList.AddFirst(new Item(3, "Head n Shoulders")); TestLinkedList.AddAfter(TestLinkedList

C#中LinkedList的Find()方法适用于字符串等,但如何将其用于结构、对象等

这是我的密码:

    {
        LinkedList<Item> TestLinkedList = new LinkedList<Item>();

        TestLinkedList.AddFirst(new Item(3, "Head n Shoulders"));

        TestLinkedList.AddAfter(TestLinkedList.First, new Item(45, "Dell"));

        //Get the 2nd node in the linklist

        Item c = new Item(3, "Head n Shoulders");

        LinkedListNode<Item> Node2 = TestLinkedList.Find(c);



        TestLinkedList.AddAfter((Node2), new Item(32, "Adidas"));

        foreach (Item i in TestLinkedList)
        {
            i.Print();
        }

        Console.ReadKey();
    }
{
LinkedList TestLinkedList=新建LinkedList();
TestLinkedList.AddFirst(新项目(3,“头和肩”);
TestLinkedList.AddAfter(TestLinkedList.First,新项(45,“戴尔”);
//获取链接列表中的第二个节点
项目c=新项目(3,“头与肩”);
LinkedListNode节点2=TestLinkedList.Find(c);
TestLinkedList.AddAfter((Node2),新条目(32,“阿迪达斯”);
foreach(TestLinkedList中的项目i)
{
i、 打印();
}
Console.ReadKey();
}

Node2的返回值为NULL。我没有使用唯一的Hashcode etc是否犯了错误?

为了让
查找
返回正确的对象,您的
类需要重写
等于
方法。当然,您还需要重写
GetHashCode
:虽然
LinkedList
Find
不调用
GetHashCode
,但这两种方法需要一起更改:

重写
Equals(Object)
的类型也必须重写
GetHashCode
;否则,哈希表可能无法正常工作