Dusan
Dusan

Reputation: 5144

Why does object comparison in C# work strange?

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.

Upvotes: 0

Views: 299

Answers (6)

Kendall Frey
Kendall Frey

Reputation: 44326

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.

Upvotes: 9

Habib
Habib

Reputation: 223267

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

Upvotes: 7

Oscar Foley
Oscar Foley

Reputation: 7025

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

Upvotes: 0

Falaque
Falaque

Reputation: 896

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

Upvotes: 0

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

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

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

Why is this happening?

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

In string == compares their values.

Try Var1.Equals(Var2);

Upvotes: 0

Related Questions