Operator overloading (== and !=) and null

W

Wilhelm Heramb

What is the best practice to implement operator overloading for == and !=
that handles null on either lhs or rhs.


Andreas :)
 
G

Guest

Do you mean "Should I handle null on either lhs, or rhs?". Answer: do
whatever the Object "==" operator does (it handles null on either side).

Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...

if (l == null)
if (r == null)
return true;
else
return false;
else

etc..etc..

Anything else could go wrong, or could confuse a reader.
 
K

Kevin Yu [MSFT]

Hi Andreas,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to the best practice for
operator overloading. If there is any misunderstanding, please feel free to
let me know.

To overload operators that handles nulls, you need to check for null in the
operator method. You can take a look at the following link for tutorial on
operator overloading.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vcwlkoperatoroverloadingtutorial.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
W

Wilhelm Heramb

Thanks for the support guys :)

The following does not work if ie. rhs is null;

public static bool operator ==(PreparePhone lhs, PreparePhone rhs)
{
if(lhs.Number == rhs.Number && lhs.NumberType == rhs.NumberType)
return true;
else
return false;
}

public static bool operator !=(PreparePhone lhs, PreparePhone rhs)
{
return (lhs.Number != rhs.Number || lhs.NumberType != rhs.NumberType);
}

public override bool Equals(object phone)
{
return ((this.Number.Equals(((PreparePhone)phone).Number))
&& (this.NumberType.Equals(((PreparePhone)phone).NumberType)));
}
 
G

Guest

Hi Kevin.

cute sig, btw.. :)

You might be interested to know that the link doesn't work for me (and
probably anyone outside the MS VPN)
 
K

Kevin Yu [MSFT]

Hi,

Please pay attention to the word wrap. The link contains 2 lines.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

James Curran

Javaman59 said:
Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...

if (l == null)
if (r == null)
return true;
else
return false;
else


The problem with that is, since this is being done within an overloaded
operator==, "if (l == null)" results in an infinite loop.

What one needs to do is:

if (Object.ReferenceEqual(l, null))
// etc

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
O

Octavio Hernandez

James,

I think you're right in that the code as it stands produces an infinite
loop.
I have seen somewhere the problem solved this way:

object ol = l, or = r;
if (ol == null || or == null) // here the System.Object operator == is
used
return false;
else {
// here the "real" comparison
}

Regards - Octavio

"if (l == null)" results in an infinite loop.
 
G

Guest

Thanks James & Octavio.

I've tested my code, and indeed it does produce infinite recursion. For a
solution, I prefer the (object) conversion to ReferenceEqual, just because we
do a few of them, and its shorter.

I notice that Octavio's example returns false for == if both sides are null.
I don't think this is correct, and is not consistent with the System.Object
== operator.

This works...

class MyInt
{
int myValue;

public static bool operator ==(MyInt lhs, MyInt rhs)
{
// If either side is null, then only return true if
// both sides are null.
object ol = lhs;
object or = rhs;
if ((ol == null) || (or == null))
return ((ol == null) && (or == null));

else

// lhs & rhs not null. Compare instance values.
return lhs.myValue == rhs.myValue;
}

.... plus a couple of functions required by the compiler
}



James Curran said:
Javaman59 said:
Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...

if (l == null)
if (r == null)
return true;
else
return false;
else


The problem with that is, since this is being done within an overloaded
operator==, "if (l == null)" results in an infinite loop.

What one needs to do is:

if (Object.ReferenceEqual(l, null))
// etc

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
O

Octavio Hernandez

Oops! I did it again! :)
The correct version (I hope) is:

object ol = l, or = r;
if (ol == null)
if (or == null)
return true;
else
return false;
else

if (or == null)
return false;
else {
// compare "l" and "r"
}

Thanks for pointing it out.

Regards - Octavio

Javaman59 said:
Thanks James & Octavio.

I've tested my code, and indeed it does produce infinite recursion. For a
solution, I prefer the (object) conversion to ReferenceEqual, just because
we
do a few of them, and its shorter.

I notice that Octavio's example returns false for == if both sides are
null.
I don't think this is correct, and is not consistent with the
System.Object
== operator.

This works...

class MyInt
{
int myValue;

public static bool operator ==(MyInt lhs, MyInt rhs)
{
// If either side is null, then only return true if
// both sides are null.
object ol = lhs;
object or = rhs;
if ((ol == null) || (or == null))
return ((ol == null) && (or == null));

else

// lhs & rhs not null. Compare instance values.
return lhs.myValue == rhs.myValue;
}

... plus a couple of functions required by the compiler
}



James Curran said:
Javaman59 said:
Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...

if (l == null)
if (r == null)
return true;
else
return false;
else


The problem with that is, since this is being done within an
overloaded
operator==, "if (l == null)" results in an infinite loop.

What one needs to do is:

if (Object.ReferenceEqual(l, null))
// etc

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top