473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Boxing causing problem during comparisons

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.
Nov 17 '05 #1
4 1522
You coded a routine for the equals operation that you'd rather use... why
not use it instead of the == operator?
Or just a.Equals(b)?

The behavior you observed is expected and well documented when comparing
object references.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Aamir Mahmood" <a@b.c> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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.

Nov 17 '05 #2
Aamir Mahmood <a@b.c> wrote:
What I have concluded that MS has wrongly implemented the == operator for
object class. It should have been something like this
No, you just wrongly interpreted it, IMO.
------
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?


If you effectively want Equals to be called, just call Equals yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
> If you effectively want Equals to be called, just call Equals yourself.
But Equals cannot be called on a null object. Why can't it be a default
implementation in object class? As I said it has no harm. and writing a
null safe code before calling Equals involves 5-6 extra lines just for
comparison of boxed variables

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Aamir Mahmood <a@b.c> wrote:
What I have concluded that MS has wrongly implemented the == operator for
object class. It should have been something like this


No, you just wrongly interpreted it, IMO.
------
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?


If you effectively want Equals to be called, just call Equals yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4
Aamir Mahmood <a@b.c> wrote:
If you effectively want Equals to be called, just call Equals yourself.
But Equals cannot be called on a null object.
So call the static object.Equals (x, y) instead.
Why can't it be a default implementation in object class?
Direct reference comparisons are very cheap, so using them where
possible is a win.
As I said it has no harm. and writing a
null safe code before calling Equals involves 5-6 extra lines just for
comparison of boxed variables


No it doesn't - just use object.Equals if you don't know whether either
of them are non-null. If you know that one is non-null (x, say), just
call x.Equals(y) and you'll get false back if y is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
1
by: Tom | last post by:
Couple of questions relating to boxing. Firstly, I already know that boxing is the processing of temporarily copying a ValueType (e.g. struct, enum) to the heap so that the system can treat a...
7
by: J.Marsch | last post by:
Hello all: I am trying to introduce the concept of boxing (and some of the hang-ups) to some developers that are coming onto a project. A while back, I read a really cool article that was...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
4
by: Peter Olcott | last post by:
I want to be able to make my .NET applications run just as fast as unmanaged C++. From my currently somewhat limited understanding of the .NET framework and the C# language, it seems that...
94
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. ...
19
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.