Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
C#
static void Main(string[] args)
      {
          Object obj = "Int32";
          String s = "Int32";
          String s1 = typeof (int).Name;
          Console.WriteLine("{0}",obj==s);     //true
          Console.WriteLine("{0}", s == s1);  //True
         <big> Console.WriteLine("{0}", obj == s1);//False     *why ???</big>
          Console.ReadLine();
      }



i am not able to understand clearly why the "s1 == obj" is false

what is rule or which basis they are comparing.
Posted
Updated 14-Sep-14 22:24pm
v2

This is a very curious case of outward violation of the transitive property of equality.
I suppose it is due to the nature of C# strings.

C#
Object obj = "Int32";
Here the string is created and 'internalized'. A reference to is is assigned to obj.

C#
String s = "Int32";
Here, the same reference assigned to obj is assigned to s (they 'points' to the same internalized string).

C#
Console.WriteLine("{0}",obj==s);     //true
Here obj and s are compared by reference. The result is true because they both hold the same reference.

C#
Console.WriteLine("{0}", s == s1);  //True
Here s and s1 are compare by value because both are string.

C#
Console.WriteLine("{0}", obj == s1);//False     *why ???
Here obj and s1 are compared by reference and the result is false because they don't hold the same reference.
 
Share this answer
 
Comments
Shmuel Zang 15-Sep-14 6:40am    
5'ed. See my addition.
siddharth629 15-Sep-14 7:27am    
thanks sir.
CPallini 15-Sep-14 7:32am    
You are welcome.

In addition to Solution 2, that happens because of a different implementation of the operator ==. While for other reference types, it behaves like ReferenceEquals, for the String class, the Equality Operator is implemented to use the Equals method instead.


When you compare the strings using a reference to an Object, the compiler calls the equality operator of Object (and compares references). When you compare the strings using a reference to a String, the compiler calls the equality operator of String (and compares values).

 
Share this answer
 
v2
Comments
siddharth629 15-Sep-14 7:27am    
thanks sir.
CPallini 15-Sep-14 7:32am    
My 5.
The data type Object is not the same as a String.
It is like comparing a vehicle and a car. A car is a vehicle, but the other way around is not necessarily true.
The same goes for programming. A String inherits from Object, so a String is an Object but an Object could be basically any type.

Try this instead.
C#
Console.WriteLine("{0}", obj.ToString() == s1);


I am not 100% sure, but I think the reason is that for the first two variables, a constant string with the value "Int32" is allocated on the heap. As this string is only allocated once, the comparison
obj == s is true because it is the same string with two different pointers pointing to the same data.

This construct results in a value allocated on the stack.
C#
String s1 = typeof (int).Name;

The comparison s == s1 works because now you compare two string types.

When comparing obj == s1, you try to compare two different types.

This explains it better. String.Intern Method[^]

(My internet connection has been slow, so I might have missed some comments that describes this)
 
Share this answer
 
v4
Comments
siddharth629 15-Sep-14 4:36am    
sir,

then Why obj==s is true . and s==s1 is also true


please don't get irritated.
Niranjankr 15-Sep-14 4:41am    
Because s and s1 is kept in same type String.
siddharth629 15-Sep-14 4:47am    
AND then WHY obj=s please explain it in deep or if you have a link regarding that the please send me
George Jonsson 15-Sep-14 5:01am    
See my updated answer.
siddharth629 15-Sep-14 7:27am    
thanks sir.

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