473,569 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne("a==b " + (a==b)); // prints False

Console.WriteLi ne("c==d " + (c==d)); // prints False

Console.WriteLi ne("i==j " + (i==j)); // prints True

Console.WriteLi ne("e==f " + (e==f)); // prints False

Console.WriteLi ne("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.Referenc eEquals(a, null);
bool bNull = object.Refernec eEquals(b, null);

if (aNull && bNull) return true;
if (aNull || bNull) return false;

if (!object.Refere nceEquals(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 1531
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******** **********@TK2M SFTNGP15.phx.gb l...
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.WriteLi ne("a==b " + (a==b)); // prints False

Console.WriteLi ne("c==d " + (c==d)); // prints False

Console.WriteLi ne("i==j " + (i==j)); // prints True

Console.WriteLi ne("e==f " + (e==f)); // prints False

Console.WriteLi ne("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.Referenc eEquals(a, null);
bool bNull = object.Refernec eEquals(b, null);

if (aNull && bNull) return true;
if (aNull || bNull) return false;

if (!object.Refere nceEquals(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.Referenc eEquals(a, null);
bool bNull = object.Refernec eEquals(b, null);

if (aNull && bNull) return true;
if (aNull || bNull) return false;

if (!object.Refere nceEquals(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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.Referenc eEquals(a, null);
bool bNull = object.Refernec eEquals(b, null);

if (aNull && bNull) return true;
if (aNull || bNull) return false;

if (!object.Refere nceEquals(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.co m>
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.co m>
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
323
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
43
6853
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 another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app...
1
2212
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 value type like a reference type. However, I have some questions relating to implicit boxing: 1. If I add custom instance method on a struct, will it...
7
1477
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 formatted as a quiz. It introduced a number of the weird little nuances that you run into when implicit boxing occurs in your code (like values not...
24
2587
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 you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
4
2960
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 Boxing/Unboxing might present of problem. Since C++ has pointer syntax, I was thinking that this might eliminate the need for Boxing and Unboxing. Am I...
94
5624
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. Thanks.
19
13693
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
7747
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 biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every...
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.