I have a condition in a silverlight application that compares 2 strings, for some reason when I use ==
it returns false while .Equals()
returns true.
Here is the code:
if (((ListBoxItem)lstBaseMenu.SelectedItem).Content.Equals("Energy Attack"))
{
// Execute code
}
if (((ListBoxItem)lstBaseMenu.SelectedItem).Content == "Energy Attack")
{
// Execute code
}
Any reason as to why this is happening?
I would add that if you cast your object to a string then it will work correctly. This is why the compiler will give you a warning saying:
There is another dimension to an earlier answer by @BlueMonkMN. The additional dimension is that the answer to the @Drahcir's title question as it is stated also depends on how we arrived at the
string
value. To illustrate:The output is:
==
The == operator can be used to compare two variables of any kind, and it simply compares the bits.
Note : there are more zeroes on the left side of the int but we don't care about that here.
int a (00000011) == byte b (00000011)
Remember == operator cares only about the pattern of the bits in the variable.
Use == If two references (primitives) refers to the same object on the heap.
Rules are same whether the variable is a reference or primitive.
a == c is true a == b is false
the bit pattern are the same for a and c, so they are equal using ==.
Equal():
Use the equals() method to see if two different objects are equal.
Such as two different String objects that both represent the characters in "Jane"
When comparing an object reference to a string (even if the object reference refers to a string), the special behavior of the
==
operator specific to the string class is ignored.Normally (when not dealing with strings, that is),
Equals
compares values, while==
compares object references. If two objects you are comparing are referring to the same exact instance of an object, then both will return true, but if one has the same content and came from a different source (is a separate instance with the same data), only Equals will return true. However, as noted in the comments, string is a special case because it overrides the==
operator so that when dealing purely with string references (and not object references), only the values are compared even if they are separate instances. The following code illustrates the subtle differences in behaviors:The output is:
The
==
token in C# is used for two different equality-check operators. When the compiler encounters that token, it will check whether either of the types being compared has implemented an equality-operator overload for either the specific combination types being compared(*), or for a combination of types to which both types can be converted. If the compiler finds such an overload it will use it. Otherwise, if the two types are both reference types and they are not unrelated classes (either may be an interface, or they may be related classes), the compiler will regard==
as a reference-comparison operator. If neither condition applies, compilation will fail.Note that some other languages use separate tokens for the two equality-check operators. In VB.NET, for example, the
=
token is used within expressions solely for the overloadable equality-check operator, andIs
is used as a reference-test or null-test operator. An to use=
on a type which does not override the equality-check operator will fail, as will attempting to useIs
for any purpose other than testing reference equality or nullity.(*)Types generally only overload equality for comparison with themselves, but it may be useful for types to overload the equality operator for comparison with other particular types; for example,
int
could have (and IMHO should have but didn't) defined an equality operators for comparison withfloat
, so that 16777217 would not report itself equal to 16777216f. As it is, since no such operator is defined, C# will promote theint
tofloat
, rounding it to 16777216f before the equality-check operator sees it; that operator then sees two equal floating-point numbers and reports them as equal, unaware of the rounding that took place.Firstly, there is a difference. For numbers
And for strings
In both cases,
==
behaves more usefully than.Equals