473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

ArrayList.Contains behaviour

Hi,

I just noticed that ArrayList.Contains(Object obj) uses the Equals
method of the object that is passed to the Contains method to check
for equality with the object in the list. Wouldn't it be more logical
to use the Equals method of the objects in the list?

Regards,
Ton van den Heuvel
Jul 21 '05 #1
3 5262
Ton van den Heuvel <bl*****@hotmail.com> wrote:
I just noticed that ArrayList.Contains(Object obj) uses the Equals
method of the object that is passed to the Contains method to check
for equality with the object in the list. Wouldn't it be more logical
to use the Equals method of the objects in the list?


Why? They should be identical, and if you think about it from the JIT's
point of view, it only needs to do one lookup to find the method
implementation, and then call that single method multiple times with
different parameters, which I would imagine is more efficient
(certainly in terms of the code cache locality of reference) than
looking up lots of different methods and calling each of those with the
same parameter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Jon Skeet <sk***@pobox.com> wrote in message news:<MP************************@news.microsoft.co m>...
Ton van den Heuvel <bl*****@hotmail.com> wrote:
I just noticed that ArrayList.Contains(Object obj) uses the Equals
method of the object that is passed to the Contains method to check
for equality with the object in the list. Wouldn't it be more logical
to use the Equals method of the objects in the list?


Why? They should be identical, and if you think about it from the JIT's
point of view, it only needs to do one lookup to find the method
implementation, and then call that single method multiple times with
different parameters, which I would imagine is more efficient
(certainly in terms of the code cache locality of reference) than
looking up lots of different methods and calling each of those with the
same parameter.


Good point. In terms of efficiency the current method is the best one.
Since it isn't properly described in the documentation which Equals
method is used, it caused some trouble finding out I actually had to
override the Equals method of the object passed to the Contains
method.

Regards,
Ton van den Heuvel
Jul 21 '05 #3
Ton van den Heuvel <bl*****@hotmail.com> wrote:
Why? They should be identical, and if you think about it from the JIT's
point of view, it only needs to do one lookup to find the method
implementation, and then call that single method multiple times with
different parameters, which I would imagine is more efficient
(certainly in terms of the code cache locality of reference) than
looking up lots of different methods and calling each of those with the
same parameter.


Good point. In terms of efficiency the current method is the best one.
Since it isn't properly described in the documentation which Equals
method is used, it caused some trouble finding out I actually had to
override the Equals method of the object passed to the Contains
method.


But if you'd been following the contract of Equals, it shouldn't have
made any difference. From the documentation:

<quote>
The following statements must be true for all implementations of the
Equals method. In the list, x, y, and z represent object references
that are not a null reference (Nothing in Visual Basic).

<snip>

x.Equals(y) returns the same value as y.Equals(x).
</quote>

Given that contract, Hashtable shouldn't have to specify which version
is used - the result should be the same.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Iulian Ionescu | last post by:
I have the following problem. I derived a class from ArrayList to provide some additional events I need. Then I derived 2 different classes from this one and each of them defines an indexer that...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
9
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Ton van den Heuvel | last post by:
Hi, I just noticed that ArrayList.Contains(Object obj) uses the Equals method of the object that is passed to the Contains method to check for equality with the object in the list. Wouldn't it...
12
by: Robert Hooker | last post by:
We are moving to .NET2.0 - and have noticed that .Contains() seems to be around 20% slower under NET2, compared to NET1.1. This code shows the issue: static void Main(string args) {...
14
by: Kym | last post by:
Hey, I have an arraylist which I have added a number of objects to (all the same type of object), I need to be able to check if the array already contain the object before adding it, I have tried...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.