|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why is there no Equals() or GetHashCode() in ArrayList?How is the developer supposed to check if an array list is equal to another array list? Is the developer supposed to iterate through the array list to find out if they are equal or not? This should be handled by the framework classes and I don't want to do any unnecessary implementation (read copy-paste-problems). If I look through the other classes in System.Collection I see that no implements GetHashCode() or Equals(). If none of them do, then it must be a design choice. But what design choice would that be? If anyone have any explanation, please let me know.I really want to know. There is an interface in System.Collections.Generic that is named IEqualityComparer, which has two methods, Equals() and GetHashCode(). Why make an interface when every class implements those methods? And why doesn't the default list/collection classes implement this interface? <flamebait-to-be-ignored> If I look to Java, the ArrayList implements equals(), hashCode() as all good objects should. They have also a good explanation on how it is done here. http://shorl.com/gogedrikekati http://shorl.com/fygafrebrypeko </flamebait-to-be-ignored> Sorry that this post was double posted into the usenet aspnet group. Regards http://redsolo.blogspot.com 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. 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. Ciaran O'Donnell http://wannabedeveloper.spaces.live.com Show quote "eramf***@gmail.com" wrote: > Why doesnt ArrayList implements the Equals() or GetHashCode() methods? > How is the developer supposed to check if an array list is equal to > another array list? Is the developer supposed to iterate through the > array list to find out if they are equal or not? This should be handled > by the framework classes and I don't want to do any unnecessary > implementation (read copy-paste-problems). > > If I look through the other classes in System.Collection I see that no > implements GetHashCode() or Equals(). If none of them do, then it must > be a design choice. But what design choice would that be? If anyone > have any explanation, please let me know.I really want to know. > > There is an interface in System.Collections.Generic that is named > IEqualityComparer, which has two methods, Equals() and GetHashCode(). > Why make an interface when every class implements those methods? And > why doesn't the default list/collection classes implement this > interface? > > <flamebait-to-be-ignored> > If I look to Java, the ArrayList implements equals(), hashCode() as all > good objects should. They have also a good explanation on how it is > done here. > http://shorl.com/gogedrikekati > http://shorl.com/fygafrebrypeko > </flamebait-to-be-ignored> > > Sorry that this post was double posted into the usenet aspnet group. > > Regards > http://redsolo.blogspot.com > > Ciaran O''Donnell skrev:
> What would the arraylist return for GetHashCode, would it aggregate the If you check the two links my original post contained, you can read> 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. 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. > In .NET 2, the list has the True for all predicate which you could pass an Yes, that would be less code. But it would be much less code if the> 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. 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 You can always subclass or wrap the .NET collections to add this yourself.
I'm sure MS had a reason not to include it and I think it was probably because people would mistake the true function of the method and use it wrong. I have overridden equals in possibly every project I have worked on. It enables so many things for you, especially working with business objects and collections. Show quote "eramf***@gmail.com" wrote: > Ciaran O''Donnell skrev: > > 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. > > > 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 > > That is why I posted this question, to find out the reason to why they
choose not to implement it. But I still havent found a reason for it, but it seems that they didn't want to implement it on collections, since it is not a pure value type. But in my eyes ArrayList is just a sequence of (potential) value types, and therefore it should implement it. But, if anyone knows. Please let me know. Ciaran O''Donnell wrote: > You can always subclass or wrap the .NET collections to add this yourself. Impl. Equals/GetHashCode() sure helps when Im unit testing.> I'm sure MS had a reason not to include it and I think it was probably > because people would mistake the true function of the method and use it > wrong. > I have overridden equals in possibly every project I have worked on. It > enables so many things for you, especially working with business objects and > collections. //E |
|||||||||||||||||||||||