I need to have instances of a DispenseFile class that inherits from DispenseEntity that implements IDispenseEntity use a custom equality for purposes of comparing elements in List.
My interface is:
public interface IDispenseEntity : IEquatable<IDispenseEntity>
{
byte[] Id { get; set; }
List<byte[]> Key { get; }
List<byte[]> ParentKey { get; set; }
double Volume { get; set; }
public bool DispenseEnabled { get; set; }
string Name { get; set; }
}
My DispenseEntity class:
public class DispenseEntity : IDispenseEntity, IEquatable<IDispenseEntity>
{
//Other properties and methods
//I've tried - this implements IEquatable:
public bool Equals(IDispenseEntity other)
{
return Id.SequenceEqual(other.Id);
}
//I've tried - this overrides default:
public override bool Equals(object obj)
{
return Id.SequenceEqual((obj as IDispenseEntity).Id);
}
}
My DispenseFile class:
public class DispenseFile : DispenseEntity, IParent, IOutputable, IDispenseFile
{
//Other methods and properties
public override bool Equals(object obj)
{
return base.Equals(obj);
}
}
No matter what I use in DispenseEntity class for Equals() method it does not get used when I try:
List<IDispenseEntity> before = _aList;
List<IDispenseEntity> after = _bList;
var intersect = before.Intersect(after).ToList();
The intersect list has zero elements.
I am absolutely positive that both _aList and _bList have an instance of DispenseFile that inherits DispenseEntity who implements IDispenseEntity. I have written test code that finds the only DispenseFile entity in _aList and finds a single instance of DispenseFile in _bList. Both of these instances are created separately and have identical property Id ( new byte[] {1,2,3,4}
)
I have tried overriding Equals. I have tried adding IEquatable to the base class and implementing the equals and GetHashCode and those don't get used.
The problem has to be me, what am I doing wrong?