Ciaran O''Donnell skrev:
[quoted text, click to view] > What would the arraylist return for GetHashCode, would it aggregate the
> hascodes of your classes. As for equals, would you have it check to see if
> all of the elements are equal, should they be equal in reference or in value,
> and does that make the arraylist equal or is it equal if it points to the
> same actual arraylist. The last one is the one the COR team chose and if you
> want to have a difference meaning for equals then you should implement it.
If you check the two links my original post contained, you can read
another approach to this. They would aggregate the hashcodes of the
contained objects and return that. That way you can have two lists
generate the same hashcode if they contains "equal" objects. Which I
needed as I had two array lists at the server and client, and the
simplest way to check if they were equal would be by using the
GetHashCode().
For equality, it is very simple. The list should go through the
contained objects and check if they are "equal". Now it is up to the
contained class/objects to define "equality". The objects/classes can
define if they should be equal in value or in reference. That way the
framework gives the option to the developer to implement Equals() or
not. But unfortunately this decision is already made for the developer.
The default equals method is to check if the two object references are
the same. That is exactly similar to the "obj1 == obj2" statement. In
my view all good classes should implement Equals() (and therefore
GetHashCode()), so the user of a class can make a choice in either
check for reference equality OR value equality.
[quoted text, click to view] > In .NET 2, the list has the True for all predicate which you could pass an
> anonymous method to to compare is to the first element, this would be like
> the equals you are after but in a short bit of code.
Yes, that would be less code. But it would be much less code if the
Collection classes implemented Equals(). Right?
It is a nice feature that developers can easily change the equality
meaning, but I havent done so in a real project yet.
//E