Possible Duplicate:
== or .Equals()
why use Equals()
and not use ==
for (i = 0; i < names.Length; i++)
{
if (names[i].Equals(targetName))
{
index = i;
break;
}
}
Possible Duplicate:
== or .Equals()
why use Equals()
and not use ==
for (i = 0; i < names.Length; i++)
{
if (names[i].Equals(targetName))
{
index = i;
break;
}
}
The ==
operator most often checks for memory equality.
If the two objects you are checking are objects (or pointers), then this compares the address that the object is at.
The .Equals()
functions is implemented by classes to check for equality between two objects. This function (where implemented in a class) will check data values in the objects instead of the memory address they live in.
Some objects override the ==
operator (see the answer here), so both methods of checking for equality may work the same for some, but not all objects. Because of this, it's safer to use .Equals()
.