簡體   English   中英

一旦項目在集合中,C# ContainsKey 返回 true

[英]C# ContainsKey returns true once an item is in the Collection

我很確定我的問題來自一個非常愚蠢的錯誤,但我找不到它......

我使用自定義類作為 SortedList 中的鍵(我也嘗試過 SortedDictionary)。 添加第一個項目沒有問題,但是當我嘗試添加第二個 Item ContainsKey() 時返回 true。

我用作鍵的類覆蓋了 Equals() 和 GetHashCode()。 我檢查了實際比較的項目,這就是我發現的:手動調用 Equals() 來比較兩個項目工作得很好,但是當它通過 ContainsKey 調用時,對象將自身與自身的相同或另一個實例進行比較。 我確保檢查要添加的對象是否確實是一個新對象,它是...

這是 Key-Class

using System;
using System.Collections;
using System.Collections.Generic;
using TFVR.Components.Gaia.Content.Element;
using UnityEngine;

namespace TFVR.Components.Gaia.Content.QueueUi
{
    public class QueueEntryElement : IComparable
    {
        public string Created;
        public string ProductId;
        public ContactInformationElement ContactInformation;
        public int Priority;
        public string OrderId;

        public QueueEntryElement(string created, string productId
            , ContactInformationElement contactInformation, int priority
            , string orderId)
        {
            Created = created;
            ProductId = productId;
            ContactInformation = contactInformation;
            Priority = priority;
            OrderId = orderId;
        }

        public int CompareTo(object obj)
        {
            if (obj == null) return 1;

            QueueEntryElement otherQueueEntryElement = obj as QueueEntryElement;
            if (otherQueueEntryElement != null)
                return this.Priority.CompareTo(otherQueueEntryElement.Priority);
            else
                throw new ArgumentException("Object is not a QueueEntryElement");
        }
        public override bool Equals(object obj)
        {
            if ((obj == null) || !this.GetType().Equals(obj.GetType()))
            {
                return false;
            }
            else
            {
                QueueEntryElement e = (QueueEntryElement)obj;
                return (this.OrderId == e.OrderId);
            }
        }
        public override int GetHashCode()
        {
            return OrderId.GetHashCode();
        }
        public override string ToString()
        {
            string str = "Created: "
                + Created + ", "
                + "Product Id: "
                + ProductId + ", "
                + "Contact Information: "
                + "{" + ContactInformation + "}" + ", "
                + "Priority: "
                + Priority + ", "
                + "Order Id: "
                + OrderId;
            return str;
        }
    }
}

這是我嘗試添加到 SortedList 的代碼

SortedList<QueueEntryElement, string> dict = new SortedList<QueueEntryElement, string>();
private void add(QueueEntryElement q, string 
{
        if (!dict.ContainsKey(q))
        {
            dict.Add(q, s);
        }
}

ContactInformationElement c1 = new ContactInformationElement("a","b","c","d","e");
QueueEntryElement e1 = new QueueEntryElement("a","b", c1, 0,"123");

ContactInformationElement c2 = new ContactInformationElement("f", "g", "h", "i", "j");
QueueEntryElement e2 = new QueueEntryElement("c", "d", c2, 0, "234");

add(e1,"one");
add(e2,"two");

這里的問題是 SortedList.ContainsKey 使用 CompareTo... NOT Equals 來確定存在。

這意味着您基本上使用 Priority 作為 Key NOT OrderId。

因此,就您的示例而言,實際的關鍵是優先級。

因此,如果您的項目沒有唯一的優先級值,那么它們將不會被添加到“字典”中。

這是 C# Generic SortedList 的正常行為。

我添加了一些虛擬代碼,以防有人仍然對測試感興趣。 為了解決我的問題,我只是將我的 CompareTo() 更改為:

public int CompareTo(QueueEntryElement obj)
        {
            if (obj == null) return 1;
            QueueEntryElement otherQueueEntryElement = obj as QueueEntryElement;
            if (otherQueueEntryElement != null)
            {
                if (Priority.CompareTo(otherQueueEntryElement.Priority) == 0)
                {
                    return OrderId.CompareTo(otherQueueEntryElement.OrderId);
                }
                return 0;
            }
            else
                throw new ArgumentException("Object is not a QueueEntryElement");
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM