简体   繁体   English

ContainsKey()在字典中找不到对象

[英]ContainsKey() dont find an object in Dictionary

I was a homework, I had to implement some method. 我是一个家庭作业,我必须实施一些方法。 I can do the upload method, where I had to upload an dictionary in elements. 我可以执行上载方法,在该方法中,我必须上载元素中的字典。 If the Key had already in the Dictionary I had to update the Value. 如果密钥已经在字典中,那么我必须更新值。 If the key wasn't in the Dictionary, I had to add that element. 如果键不在词典中,则必须添加该元素。 In Foreach I could do that. 在Foreach中,我可以这样做。

In the method never true in the if case, only the else case execute. 在if情况下,该方法永远不会为true,只有else情况下才执行。 ( In other way I tried to if(alcDictionary.Keys==alc) -but isn't work). (以其他方式,我尝试过if(alcDictionary.Keys==alc) -但不起作用)。 I don't know why. 我不知道为什么 Somebody can explain me where is my problem? 有人可以向我解释我的问题在哪里? Why never execute the If case. 为什么从不执行If案例。 (Always write out "dont find", and not the "find") (总是写出“找不到”,而不是“找到”)

I've written this method with containsKey(): 我已经用containsKey()编写了这个方法:

     public void Upload(Alcohol alc, int dl)
    {
        int d = 0;
        Alcohol s = null;
        if (alcDictionary.ContainsKey(alc))
        {

            Console.WriteLine("I find");
            d = alcDictionary[alc];
            alcDictionary[alc] = d + dl;
        }
        else
        {
            Console.WriteLine("dont find");
            alcDictionary.Add(alc, dl);
        }

With Foreach ( It works good!) 使用Foreach(效果很好!)

            int d = 0;
        Alcohol s = null;
        foreach (var item in alcDictionary)
        {
            if (item.Key.Equals(alc))
            {
                d = item.Value;
                s = item.Key;
            }
        }
        if (s != null)
        {
            alcDictionary[s] = d + dl;
        }
        else
        {
            alcDictionary.Add(alc, dl);
        }

Some other code: 其他一些代码:

    public Kocsma()
    {
        Upload(new Alcohol("Borsodi alc", 160, 4.6), 1000);
        Upload(new Alcohol("Pilsner Urquell", 250, 4.4), 800);
        Upload(new Alcohol("Soproni Ászok", 150, 4.5), 900);
        Upload(new Alcohol("Dreher Classic", 200, 5.2), 600);
    }


    static void Main(String[] args)
    {
        Alcohol b = new Alcohol("Borsodi alc", 160, 4.6); //34
        Alcohol c = new Alcohol("Bratista alc", 230, 4.5); // 51
        Alcohol d = new Alcohol("Soproni Ászok", 150, 4.5); // 33,3

        Kocsma pub = new Kocsma();

        pub.Upload(c, 300);
        pub.Upload(d, 450);
        pub.Upload(b, 100);


    }

What is alc? 什么是Alc? If it's a class, you should override .Equals and .GetHashcode for dictionaries to work properly. 如果是类,则应重写.Equals和.GetHashcode,以使字典正常工作。 If it's a structure, you don't need to worry about it. 如果是结构,则无需担心。

I'm curious why your second version works, because Item.Key.Equals(alc) should show the same behavior. 我很好奇您的第二个版本为何有效,因为Item.Key.Equals(alc)应该显示相同的行为。

You need to correctly override GetHashCode() in your key class. 您需要在键类中正确覆盖GetHashCode()

GetHashCode() must return equal values for equal objects. GetHashCode()必须为相等的对象返回相等的值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM