[C#ContainsKey一旦在收藏夹中就返回true

问题描述 投票:3回答:1

我很确定我的问题来自一个非常愚蠢的错误,但我找不到它...

我正在使用自定义类作为SortedList中的键(我也尝试过SortedDictionary)。添加第一个项目没有问题,但是当我尝试添加第二个项目时,ContainsKey()返回true。

我用作键的类覆盖了Equals()和GetHashCode()。我检查了实际比较的项目,这就是我发现的内容:手动调用Equals()比较两个项目效果很好,但是当通过ContainsKey调用该对象时,该对象会将自身与自身的相同实例或另一个实例进行比较。我确保检查要添加的对象是否确实是一个新对象,并且它是...

这是密钥类别

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");

UPDATE:由于问题被不公平地关闭,因此我决定编辑问题并在此处添加答案。这里的问题是SortedList.ContainsKey使用CompareTo ...不等于确定存在。

这意味着您基本上是使用Priority作为键NOT OrderId。

因此,出于您的示例目的,实际密钥为优先级。

因此,如果您的项目没有唯一的优先级值,那么它们将不会添加到“词典”中。

这是C#通用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");
        }
c# unity3d sortedlist
1个回答
0
投票

这里的问题是SortedList.ContainsKey使用CompareTo ...不等于确定存在。

这意味着您基本上是使用Priority作为键NOT OrderId。

因此,出于您的示例目的,实际密钥为优先级。

因此,如果您的项目没有唯一的优先级值,那么它们将不会添加到“词典”中。

这是C#通用SortedList的正常行为。

© www.soinside.com 2019 - 2024. All rights reserved.