ArrayList.Contains() -- using objects of different types
Posted on 2004-08-21
I have an ArrayList of objects called Permission. Permission.Equals() has been overriden to allow comparison with int, and works as expected.
However, ArrayList.Contains() continues to return false. Your thoughts or advice are appreciated.
Permission p = new Permission(9);
int pId = 9;
ArrayList plist = new ArrayList();
p.Equals(pId); // true
Object.Equals(p, pId); // true
pId.Equals(p); // false - as expected
Object.Equals(pId, p); // false - as expected
p.GetHashCode(); // 9
pId.GetHashCode(); // 9
plist.Contains(p); // true
plist.Contains(pId); // false
I thought .Contains() used the Equals method of the contained class? Otherwise I don't think i can make this work at all ... I'm clearly not going to rewrite the Equals() method of int?
It seems silly that ArrayList.Contains(suppliedObj) it would use Object.Equals(suppliedObj, ContainedObj)
Thanks for your thoughts,