简体   繁体   English

KeyNotFoundException:字典中不存在给定的键

[英]KeyNotFoundException : The given key was not present in the dictionary

I am receiving KeyNotFoundException whenever I try to access the key in dictionary.每当我尝试访问字典中的键时,我都会收到KeyNotFoundException

I have a PricingRules class which has a dictionary:我有一个PricingRules class,它有一本字典:

public class PricingRules
{
    Dictionary<ItemCode, Money> pricing_rules;

    public PricingRules()
    {
        pricing_rules = new Dictionary<ItemCode, Money>();
        pricing_rules.Add(new ItemCode("A"), new Money(50));
        pricing_rules.Add(new ItemCode("B"), new Money(30));
        pricing_rules.Add(new ItemCode("C"), new Money(20));
        pricing_rules.Add(new ItemCode("D"), new Money(15));
    }

    public void itemScanned(ItemCode itemCode, Money amount)
    {
        amount.Add(pricing_rules[itemCode]);
    }

    public Money AmountForItem(ItemCode itemCode)
    {
        return pricing_rules[itemCode];
    }
}

The key in the dictionary is of type ItemCode :字典中的键是ItemCode类型:

public class ItemCode
{
    private readonly string itemCode;

    public ItemCode(string itemCode)
    {
        this.itemCode = itemCode;
    }

    public override int GetHashCode()
    {
        return itemCode.GetHashCode();
    }
}

and the value is of type Money :值是Money类型:

public class Money
{
    private int amount;

    public Money(int amount)
    {
        this.amount = amount;
    }

    public override bool Equals(object obj)
    {
        Money money = (Money)obj;
        return this.amount == money.amount;
    }

    public void Add(object obj)
    {
        Money money = (Money)obj;
        this.amount += money.amount;
    }

    public override int GetHashCode()
    {
        return amount.GetHashCode();
    }
}

I have tried overriding the GetHashCode() function to return the HashCode of the primitive type in the class but it still is giving KeyNotFoundException我已经尝试覆盖GetHashCode() function 以返回 class 中原始类型的 HashCode 但它仍然给出KeyNotFoundException

The test I have written is:我写的测试是:

public class PricingRulesShould
{
    [Fact]
    void ReturnValueFiftyWhenKeyIsA()
    {
        Money expected = new Money(50);
        PricingRules pr = new PricingRules();

        ItemCode itemcode = new ItemCode("A");
        Money actual = pr.AmountForItem(itemcode);

        Assert.Equal(expected, actual);
    }
}

When Dictionary accesses its elements, it looks for the equality of keys.Dictionary访问其元素时,它会查找键的相等性。
Since the ItemCode is not a primitive type, when you compare new ItemCode("A") == new ItemCode("A") you should get false as a result, because they are different instances with identical contents.由于ItemCode不是原始类型,当您比较new ItemCode("A") == new ItemCode("A")时,您应该得到false作为结果,因为它们是具有相同内容的不同实例。
What you can do is to implement IEquatable<ItemCode> by the ItemCode class:您可以做的是通过ItemCode class 实现IEquatable<ItemCode>

public class ItemCode : IEquatable<ItemCode>
{
    private readonly string itemCode;

    public ItemCode(string itemCode)
    {
        this.itemCode = itemCode;
    }

    public override int GetHashCode()
    {
        return itemCode != null ? itemCode.GetHashCode() : 0;
    }

    public bool Equals(ItemCode other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return string.Equals(itemCode, other.itemCode);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        return obj is ItemCode ic && Equals(ic);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 KeyNotFoundException:字典中不存在给定的键 - KeyNotFoundException: The given key was not present in the dictionary C#:KeyNotFoundException:字典中不存在给定的键 - C#: KeyNotFoundException: The given key was not present in the dictionary NHibernate - KeyNotFoundException:给定的键不在字典中 - NHibernate - KeyNotFoundException: The given key was not present in the dictionary KeyNotFoundException:字典中不存在给定的键“ OpenIdConnect.Code.RedirectUri” - KeyNotFoundException: The given key 'OpenIdConnect.Code.RedirectUri' was not present in the dictionary Xamarin.Forms - KeyNotFoundException: 字典中不存在给定的键 C# - Xamarin.Forms - KeyNotFoundException: The given key was not present in the dictionary C# KeyNotFoundException:字典中不存在给定的键(C#Unity ANDROID APP) - KeyNotFoundException: The given key was not present in the dictionary (C# Unity ANDROID APP) System.Collections.Generic.KeyNotFoundException:给定的键不在字典中 - System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary System.Collections.Generic.KeyNotFoundException:字典中不存在给定的键“Item” - System.Collections.Generic.KeyNotFoundException : The given key 'Item' was not present in the dictionary System.Collections.Generic.KeyNotFoundException:字典中不存在给定键“All” - System.Collections.Generic.KeyNotFoundException: The given key 'All' was not present in the dictionary System.Collections.Generic.KeyNotFoundException:从PUSHSHARP发送推送通知时,字典中不存在给定的键 - System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary while sending Push Notifiaction from PUSHSHARP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM