Equals and "==" operator

H

#Hai

Hi,
What is the difference between Object.Equals and "==" operator ?
When we use CollectionBase.List.Remove(object), which methods is used to
compare objects ?

Thanks
 
A

Anthony Bouch

For interest, there are also performance differences between instance and
static(shared) methods for Equals and the == operator when comparing
strings.
 
M

Markus Wildgruber

Hi Anthony!

Can you tell me more about the performance differences. Which way is
fastest?

TIA,

Markus
 
1

100

Furthermore, not all languages support operator overloading. So, if one
wants to write language independent code she/he should not rely on operator
overloading. For c# convenince the operators might be overloaded and the
virtual method might be used internally.

B/rgds
100

Jon Skeet said:
[What does this have to do with Windows forms, by the way? Please limit
your post to relevant groups - preferrably only one!]

#Hai said:
What is the difference between Object.Equals and "==" operator ?

Operators are not applied polymorphically, methods are. For instance:

string x = "hello";
string y = new string (x.ToCharArray());

object a=x;
object b=y;

x==y; // True, because String== is applied
a==b; // False, because Object== is applied, which asserts reference
// identity

x.Equals(y); // True, because String.Equals override Object.Equals
a.Equals(b); // True, because methods are invoked polymorphically
When we use CollectionBase.List.Remove(object), which methods is used to
compare objects ?

Almost certainly Equals - it's the only way that makes sense, really,
given that CollectionBase only knows about Objects, so to remove
entries you'd have to have the exact reference if it used ==.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top