簡體   English   中英

KeyNotFoundException:字典中不存在給定的鍵

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

每當我嘗試訪問字典中的鍵時,我都會收到KeyNotFoundException

我有一個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];
    }
}

字典中的鍵是ItemCode類型:

public class ItemCode
{
    private readonly string itemCode;

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

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

值是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();
    }
}

我已經嘗試覆蓋GetHashCode() function 以返回 class 中原始類型的 HashCode 但它仍然給出KeyNotFoundException

我寫的測試是:

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

Dictionary訪問其元素時,它會查找鍵的相等性。
由於ItemCode不是原始類型,當您比較new ItemCode("A") == new ItemCode("A")時,您應該得到false作為結果,因為它們是具有相同內容的不同實例。
您可以做的是通過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.

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