简体   繁体   English

确定两个对象是否相等

[英]Determining if two objects are equal

I'm trying to test whether an object is equal to one in a list of objects given certain criteria (is name equal) and if it is, don't add it to list, otherwise add it. 我正在尝试在给定特定条件的情况下测试对象列表中的对象是否等于一个(名称相等),如果是,请勿将其添加到列表中,否则添加它。 I have to use a method with this signature "static int Find(List c, Coffee x)". 我必须使用带有此签名的方法“ static int Find(List c,Coffee x)”。 Find seeks x in c and returns a valid index (ie, 0, 1, …) if x exists in c, returns -1 otherwise. 如果在c中存在x,则Find在c中寻找x并返回有效索引(即0、1,…),否则返回-1。 My equals method doesn't seem to realize the names are the same when I pass exact matches. 当我传递完全匹配项时,我的equals方法似乎没有意识到名称相同。 Why is this? 为什么是这样? Here's my code: 这是我的代码:

        Coffee obv = new Coffee();
        Decaf decafCoffee = null;
        Regular regularCoffee = null;
        List<Coffee> inventory = new List<Coffee>();

        if (some sxpression)
            {
                decafCoffee = new Decaf(name, D, C, M);
                find = obv.Find(inventory, decafCoffee);
                if (find == -1)
                {
                    inventory.Add(decafCoffee);
                }
            }


          public class Coffee : IDisposable
          {
              public override bool Equals(object obj)
              {
                  if (obj is Coffee)
                  {
                    bool isNameEqual = Name.Equals(this.Name);

                 return (isNameEqual);
                  }
        return false;
    }

        public int Find(List<Coffee> c, Coffee x)
    {
        if (c.Equals(x))
        {
            return 0;
        }

        return -1;
    }
        }          

You are testing for equality on the List to an instance of Coffee. 您正在测试List上是否与Coffee实例相等。 This will always return -1. 这将始终返回-1。 What you want is c.Contains(x). 您想要的是c.Contains(x)。 Keep in mind when you override Equals you should also provide a similar override for GetHashCode(). 请记住,当您覆盖Equals时,还应该为GetHashCode()提供类似的覆盖。 Look here for Microsoft advice on implementing and overriding Equals on an object. 在此处查找有关在对象上实现和覆盖Equals的Microsoft建议

public int Find(List<Coffee> c, Coffee x) {
    return c.IndexOf(x);
}

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

Your error is here: 您的错误在这里:

public int Find(List<Coffee> c, Coffee x)
{
    if (c.Equals(x))  // <-- this will never return true
    {
        return 0;
    }

    return -1;
}

However, your Find method is unnecessary. 但是,您的Find方法是不必要的。 Use List<T>.IndexOf to keep your concept: 使用List<T>.IndexOf保持您的概念:

var index = inventory.IndexOf(decafCoffee);

Your problem is here: 您的问题在这里:

public int Find(List<Coffee> c, Coffee x)
{
    if (c.Equals(x))
    {
        return 0;
    }

    return -1;
}

c is a List<Coffee> not a Coffee object. cList<Coffee>而不是Coffee对象。

You need to change your code so that it iterates over the list to see if it contains x : 您需要更改代码,以使其遍历列表以查看其是否包含x

for (int i = 0; i < c.Count; ++i)
    if (c[i].Equals(x))
        return i;

return -1

You can do as below, since you have Equals method you can use it for finding matching item 您可以执行以下操作,因为您具有Equals方法,因此可以使用它来查找匹配项

public int Find(List<Coffee> c, Coffee x)
{
    if (c.Any(i=>i.Equals(x))
    {
        return 0;
    }

    return -1;
}

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

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