Click here to Skip to main content
16,022,536 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am new to c#. i read the article about == and equals comparison for value types and ref types.i wrote small program which is mentioned below.

C#
 int a = 3; int b = 3;
 //string a = "abc", b = "abc";
 //object a = 1; object b = 1;
// object a = "abc"; object b = "abc";

 if (a == b)
 {
     Console.WriteLine("true");
 }
 else
 {
     Console.WriteLine("false");
 }

 if (a.Equals(b))
 {
     Console.WriteLine("true");
 }
 else
 {
     Console.WriteLine("false");
 }

 if (object.Equals(a, b))
 {
     Console.WriteLine("true");
 }
 else
 {
     Console.WriteLine("false");
 }

 if (object.ReferenceEquals(a, b))
 {
     Console.WriteLine("true");
 }
 else
 {
     Console.WriteLine("false");
 }


after viewed the results i am confused difference for these four comparisons.Please tell me the difference clearly.

Results:

case 1: int a=3;int b=3;
true
true
true
false

case 2: string a="abc";string b="abc";
true
true
true
true

case 3: object a=1; object b=1;
fasle
true
true
false

case 4: object a="abc";object b="abc";
true
true
true
true
Posted
Comments
ZurdoDev 9-Jun-15 14:51pm    
Intellisense or help will explain each one.
Sergey Alexandrovich Kryukov 9-Jun-15 14:54pm    
And which of the cases are confusing?
By the way, writing

if (a == b)
WriteLine("true")

is so pointless. Write
WriteLine(a==b);

And so on...

—SA

Seems confusing, but it's because you are trying to mix Value types and Reference types and assuming the results will be the same.

== does a Value comparison for Value types, and a Reference comparison for reference types.
Equals does a Value comparison for Value types, and a Reference comparison for reference types
object.Equals is a static, null safe version of Equals
ReferenceEquals always compares references.

int is a value type, and three is three is three - but a reference comparison on a value type will always return false because they need to be "boxed" to do a reference comparison, and that produces different references.
string is a reference type, but it's a "special case" - strings are immutable so constant string values will always give you the same reference as they are stored in a "string bank" that the compiler can reuse to save space.
In order to assign a value type integer value to an object, it has to be "boxed" into a reference type. As a result, the two references aren't the same, so == and ReferenceEquals both give you false.
The last case is the second just slightly rewritten.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jun-15 15:15pm    
I'm really sorry: not true, from the first lines. Or half-true, which is still not true, but, in a way, even worse. I'll explain...
== does value comparison for value types, but not always reference comparison for reference type. For these types, the result of comparison depends on definition of equality and overridden == and !=. Object.Equals is static? Also not true. One method is virtual another one is static.
Sorry. You need to be more careful, not to confuse the beginners...
—SA
OriginalGriff 9-Jun-15 15:24pm    
https://msdn.microsoft.com/en-us/library/w4hkze5k%28v=vs.110%29.aspx
Sergey Alexandrovich Kryukov 9-Jun-15 15:27pm    
Didn't I just mentioned that? Yes, but it does not make your statement correct. You cannot say "object.Equals is a static" without telling which one, because other method System.Object.Equals is virtual.
—SA
In all your cases, a is equal to b in all senses, except referentual and one more case. I am sure that all equality cases are clear, could not cause your confusion and don't need explanation.

The only potentially confusing point could only be non-equality cases. Explaining:

In case 3, fist line: you compare 1 with 1. They are not equal because you compare the boxed form of integers, not integers themselves. As a result, you have two separate boxed objects, each having integer field of 1. As the type System.Object does not have predefined equality (say, based on some members), the comparison returns false. Different objects without overridden equality criteria are not equal.

Other cases are related to ReferenceEquals. This method is only applicable to reference types. For value types, it returns false. You can think of it as of some reasonable fallback. But the rationale is: there are no addresses in the heap at all, so there are two separate value objects with different addresses. (If you change b to 2, a will remains 1) It's logical to consider them referenctually different.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900