Two lists with identical items are not equal. What for?

I have the following:

  var a = new List<OrderRule> {
    new OrderRule("name", OrderDirection.Ascending),
    new OrderRule("age", OrderDirection.Descending)
  };


  var b = new List<OrderRule> {
    new OrderRule("name", OrderDirection.Ascending),
    new OrderRule("age", OrderDirection.Descending)
  };

  var r = a.Equals(b);

The r parameter is false, even if two lists contain elements that are equal to each other. The OrdeRule class implements IEquality. Note that the two OrderRules are equal when both directions and property are equal.

public enum OrderDirection { ASC, DESC }

public class OrderRule : IEquatable<OrderRule> {

  public OrderDirection Direction { get; }
  public String Property { get; }

  public OrderRule(String property, OrderDirection direction) {
    Direction = direction;
    Property = property;
  }

  public Boolean Equals(OrderRule other) {

    if (other == null)
      return false;

    return Property.Equals(other.Property) && Direction.Equals(other.Direction);

  }

  public override Boolean Equals(Object obj) {

    if (ReferenceEquals(null, obj))
      return false;

    if (ReferenceEquals(this, obj))
      return true;

    if (obj.GetType() != GetType())
      return false;

    return Equals(obj as IncludeRule);

  }

  public override Int32 GetHashCode() {

    return HashCode.Of(Property).And(Direction);

  }

}

What am I missing?

+4
source share
4 answers

Checking to make sure the two lists are equal does not mean checking whether they both contain the same elements.

If you do this:

var a = new List<OrderRule>();
var b = new List<OrderRule>();
var r = a.Equals(b); //false

r , . , . , a b . a - , b - .

, r :

var a = new List<OrderRule>();
var b = a;
var r = a.Equals(b) //true

, , , :

var r = a.SequenceEquals(b);

r true, Equals OrderRule, "" .


- OrderRule .

public override Boolean Equals(Object obj) {
if (ReferenceEquals(null, obj))
  return false;

if (ReferenceEquals(this, obj))
  return true;

if (obj.GetType() != GetType())
  return false;

return Equals(obj as IncludeRule);
}

, return Equals(obj as IncludeRule) return Equals(obj as OrderRule).

+7

, . .

a.Equals(b); // false
var d = a;
d.Equals(a); // true

, linq SequenceEqual

https://msdn.microsoft.com/en-us/library/bb348567(v=vs.100).aspx

+4

, new , . .

OrderRule. , OrderRule, , .

. List<OrderRule> , . , , , , , . , List , , . , , .

public static class Extensions
{
    public static bool Equal<T>(this List<T> x, List<T> y)
    {
        bool isEqual = true;
        if (x == null ^ y == null)
        {
            isEqual = false;
        }
        else if (x == null && y == null)
        {
            isEqual = true;
        }
        else if (x.Equals(y))
        {
            isEqual = true;
        }
        else if (x.Count != y.Count)
        {
            isEqual = false;
        }
        else
        {
            //This logic can be changed as per your need.
            //Here order of the list matters!
            //You can make one where order doesn't matter
            for (int i = 0; i < x.Count; i++)
            {
                if (!x[i].Equals(y[i]))
                {
                    break;
                }
            }

        }
        return isEqual;
    }
}
+1

, , . , .

0

All Articles