0

When I compare two variables typed as object and both contain same value, the comparison result using == operator produces false.

object Var1 = "X";
object Var2 = "X";

// This produces false result
bool Match = Var1 == Var2;

Why is this happening?

Edit: Above is the code that actually works!

I have based it on my real code which looks like this and does not work:

ChoiceValue = Choice.GetValue(FieldTemplate.ValueDataType);
if (ChoiceValue == Field.Value) RadioButton.IsChecked = true;

ChoiceValue is object and also the Field.Value is property typed as object.

Obviously works differently in different situations.

Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
Dusan
  • 5,000
  • 6
  • 41
  • 58
  • 1
    Please read about reference types and value types, as well as their `Equals` methods. – O. R. Mapper Jul 02 '12 at 12:29
  • My result using LINQPad: True – Kendall Frey Jul 02 '12 at 12:30
  • check this link. http://stackoverflow.com/questions/798117/comparing-2-custom-objects-c-sharp or this http://stackoverflow.com/questions/411232/comparing-two-objects – Boomer Jul 02 '12 at 12:30
  • It is so because the behaviour of `==` is defined so. This must be informative if you want to know _why_ it is defined so: http://blogs.msdn.com/b/ericlippert/archive/2009/04/09/double-your-dispatch-double-your-fun.aspx – Vlad Jul 02 '12 at 12:33
  • Original example gives the True resul, I made an edit with Real code which gives the False – Dusan Jul 02 '12 at 12:42
  • @Dusan Due to your edit, there are multiple answers now. Originally the reason would be that that the strings weren't interned was as Kendall Frey says in his answer. Now it is the much more obvious; you're using reference comparison instead of by value. – NominSim Jul 02 '12 at 12:44
  • Then WHY IS PropX == PropY different than VarX == VarY??? – Dusan Jul 02 '12 at 12:46
  • @Dusan When you use `==` you are comparing references, meaning you are looking to see if PropX refers to the **SAME** object as PropY. C# treats Strings differently though, which is why `==` works on them. – NominSim Jul 02 '12 at 12:51

6 Answers6

9

The reason this specific case returns false is because your strings are not interned. (String interning)

When I tested it, I got true, because my strings were interned.

In your case, this causes the object == operator to return false, since it compares by reference.

The reason your strings are not interned is because you are comparing dynamically built strings (meaning they were not known at compile time, but at runtime).

If you absolutely must use object variables, you can use Equals instead of ==, or you can manually intern strings with String.Intern

This case is an anomaly of the reference-typed strings trying to behave like value types. This means that they compare by value, when using the string == operator. However, when you have objects, it uses the object == operator, which compares by reference.

This is explained in the documentation for string.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
7

In your edited question you are getting this behavior because == compares the reference not their values.

In case of string values it seems to be working as expected because of string interning. Here your Var1 and Var2 points to a single copy of the string "X" and since == compares references it is giving true result.

String interning. It's a way of storing one copy of any string.

You may see: Understanding string interning

enter image description here

Habib
  • 219,104
  • 29
  • 407
  • 436
0
Why is this happening?

Because it matches reference and not its value which is false.

In string == compares their values.

Try Var1.Equals(Var2);

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Operator == compares the reference no their values.

(REFERENCE comparison)

It means that var1 is pointing to position xxxx in memory and var2 pointing to yyyy in memory. So they are different objects.

In case that your code was:

object Var1 = "X";
object Var2 = Var1;

bool Match = Var1 == Var2;

Match will be true because both Var1 and Var2 would be pointing to xxxx in memory therefore are the same object.

(Deep Object Comparison)

You can compare the values inside of the object using Equals. So when you compare Var1 (in xxxx position) and Var2(yyyy position) if it happens that both contains the same value (in this case letter X) then it would return true otherwise false. The code for doing so is:

object Var1 = "X";
object Var2 = "X";

// This produces false result
bool Match = Var1.Equals(Var2);

NOTE: this answer only works when strings are not interned. If they are interned both objects will point to same position in memory

Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
0

read more about it:

Differences in string compare methods in C#

C# object comparison

http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

Hope this helps to understand comparison in .Net

Community
  • 1
  • 1
Falaque
  • 886
  • 2
  • 12
  • 27
  • 1
    I didn't downvoted it. I guess whoever did it has as reason that you post a link about a string compares instead of object compares? Anyway.. I would compensate for it :) – Oscar Foley Jul 02 '12 at 12:39
  • I downvoted, because none of the links you provide are the reason that the OPs answer is false. Kendall Frey has it right. (Also DVd because you don't answer the question, you just list sources) – NominSim Jul 02 '12 at 12:41
  • but aren't sources use full? shouldn't you be knowing the reason, which are elaborately explained in the sources i provide? i also give the link to the related/similar questions already asked in stachoverflow, is not that fine? any way thanks for your reply. – Falaque Jul 02 '12 at 12:47
0

Because it compares the references of the object. The Best way to do it is to override Equals or else overloading == operator.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • They are both already overloaded for string so calling would solve the issue however the version of `==` used is based on the compile time type of the arguments not the runtime type and therefor in this particular case it will not affect the result – Rune FS Jul 02 '12 at 12:49