Z
Zeng
I'm trying to comparing 2 objects (pointer to object) to see if they are the
"same" as each other. Here is what the definition of being the "same"
object type for both objects, object 1, object 2, same?
-----------------------------------------------------------
Decimal 199677 199677 yes
String "hello" "hello" yes
null null
yes
I looks like I have to do 2 comparisons to cover this definition:
public bool compare( object obj1, object obj2 )
{
if( obj1 == obj2 ) // this won't detect 2 strings being the same
return true;
// assuming obj1 is not null
if( obj1.Equals( obj2 ) ) // this won't detect 2 integers being the
same!!!
return true;
}
Is that because we have value type and reference type? Notice that I haven't
consider obj1 being null yet,if not, the code can get messier ? I'm still
not believing it's that complicated to do the simple task in C#. Shouldn't
the string and integer classes both override the Equals() method to return
true when the values are the same? Would someone out there please correct
me if I'm wrong ? Thanks!
"same" as each other. Here is what the definition of being the "same"
object type for both objects, object 1, object 2, same?
-----------------------------------------------------------
Decimal 199677 199677 yes
String "hello" "hello" yes
null null
yes
I looks like I have to do 2 comparisons to cover this definition:
public bool compare( object obj1, object obj2 )
{
if( obj1 == obj2 ) // this won't detect 2 strings being the same
return true;
// assuming obj1 is not null
if( obj1.Equals( obj2 ) ) // this won't detect 2 integers being the
same!!!
return true;
}
Is that because we have value type and reference type? Notice that I haven't
consider obj1 being null yet,if not, the code can get messier ? I'm still
not believing it's that complicated to do the simple task in C#. Shouldn't
the string and integer classes both override the Equals() method to return
true when the values are the same? Would someone out there please correct
me if I'm wrong ? Thanks!