Advantage to using equals()

N

nyousfi

Hi

Whats the advantage in using .equals when comparing strings rather
than the = sign?

Thanks

NY

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
C

Cor Ligthert

Whats the advantage in using .equals when comparing strings rather
than the = sign?

I saw just in another newsgroup that somebody is paid per line that he
writes in his programs and need a general overal project rowcount for that.

It can be an advantage maybe when you are paid per character you type. For
the rest I see nothing, it needs to do a method to do the evaluation which
has surely something as the = sign in it.

Cor
 
D

Dennis Myrén

There is no significant difference.
However, using the instance method Equals should save you a couple of
function calls,
and i would claim it is therefore faster, although i have never actually
tried it out.
 
C

cody

the difference between equals() method and operator == ist that the first
one is virtual while the latter is not:

object o == "Hello";
o.equals(string.concat("He", "llo")); // will return true
o==string.concat("He", "llo") // will return false

So when using equals() it doesn't make any difference which type (object)
the variable is, only the actual type counts (string). If you use operator
== on a string variable and a object variable operator==(object, object) is
called which returns only true if and only if the objects are the same (the
same address).
 
J

Jon Skeet [C# MVP]

cody said:
the difference between equals() method and operator == ist that the first
one is virtual while the latter is not:

object o == "Hello";
o.equals(string.concat("He", "llo")); // will return true
o==string.concat("He", "llo") // will return false

So when using equals() it doesn't make any difference which type (object)
the variable is, only the actual type counts (string). If you use operator
== on a string variable and a object variable operator==(object, object) is
called which returns only true if and only if the objects are the same (the
same address).

Fortunately, the C# compiler will pick up the above and give you a
warning. This at least helps to reduce the occurrence of bugs caused by
this behaviour.
 
C

Cor Ligthert

cody said:
the difference between equals() method and operator == ist that the first
one is virtual while the latter is not:

object o == "Hello";
o.equals(string.concat("He", "llo")); // will return true
o==string.concat("He", "llo") // will return false

So when using equals() it doesn't make any difference which type (object)
the variable is, only the actual type counts (string). If you use operator
== on a string variable and a object variable operator==(object, object)
is
called which returns only true if and only if the objects are the same
(the
same address).

--
Fortunately, the VB IDE will pick up the above and give you direct a
warning and wont build. In VBNet is needed to use the IS operator to compare
the adresses of Objects to give a good distinct between values. This at
least helps to reduce the occurrence of bugs caused by
this behaviour.

(You can set those warnings by option by the way in VBNet).

:)

Cor
 

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

Top