A
Aamir Mahmood
Hi
I am on framework version 1.1 (SP1). Consider the following code.
public static void Main() {
int i=1;
int j=1;
object a=1;
object b=1;
object c=i;
object d=j;
object e=i;
object f=i;
object g=b;
object h=b;
Console.WriteLine("a==b " + (a==b)); // prints False
Console.WriteLine("c==d " + (c==d)); // prints False
Console.WriteLine("i==j " + (i==j)); // prints True
Console.WriteLine("e==f " + (e==f)); // prints False
Console.WriteLine("g==h " + (g==h)); // prints True
}
-----
Initially I got confused why these output is False for any of the above
comparisons. But I found that this is because of boxing. I don't want to
say that boxing is wrong, but what I want to say that this feature is
causing problems for me because I cannot bring the actual value out of box
in my base class before the comparison. I have an object type in my base
class and child classes can assign any value type in that.
What I have concluded that MS has wrongly implemented the == operator for
object class. It should have been something like this
------
bool aNull = object.ReferenceEquals(a, null);
bool bNull = object.ReferneceEquals(b, null);
if (aNull && bNull) return true;
if (aNull || bNull) return false;
if (!object.ReferenceEquals(a, b)) {
return a.Equals(b);
}
return true;
------
If MS had implemented the == operator for object like the fragment above,
then there would have been no harm. Since this is not the fact, therefore,
we are facing unwanted boxing problems during comparisons.
Is there any workaround for this?
-
Aamir.
I am on framework version 1.1 (SP1). Consider the following code.
public static void Main() {
int i=1;
int j=1;
object a=1;
object b=1;
object c=i;
object d=j;
object e=i;
object f=i;
object g=b;
object h=b;
Console.WriteLine("a==b " + (a==b)); // prints False
Console.WriteLine("c==d " + (c==d)); // prints False
Console.WriteLine("i==j " + (i==j)); // prints True
Console.WriteLine("e==f " + (e==f)); // prints False
Console.WriteLine("g==h " + (g==h)); // prints True
}
-----
Initially I got confused why these output is False for any of the above
comparisons. But I found that this is because of boxing. I don't want to
say that boxing is wrong, but what I want to say that this feature is
causing problems for me because I cannot bring the actual value out of box
in my base class before the comparison. I have an object type in my base
class and child classes can assign any value type in that.
What I have concluded that MS has wrongly implemented the == operator for
object class. It should have been something like this
------
bool aNull = object.ReferenceEquals(a, null);
bool bNull = object.ReferneceEquals(b, null);
if (aNull && bNull) return true;
if (aNull || bNull) return false;
if (!object.ReferenceEquals(a, b)) {
return a.Equals(b);
}
return true;
------
If MS had implemented the == operator for object like the fragment above,
then there would have been no harm. Since this is not the fact, therefore,
we are facing unwanted boxing problems during comparisons.
Is there any workaround for this?
-
Aamir.