operator== and null

G

Guest

Hi there,
Just wondering what is the correct way of implementing the comparison
operator in c#
operator==

My problem is when one or more of the objects is null,
I've cast to object to try compare them for null, trying to compare on
AlarmProfile objects will recrusivly call my comparison operator so this is
not an option.

code......
public static bool operator==(AlarmProfile lhs, AlarmProfile rhs)
{
object objLeft = lhs as object;
object objRight = rhs as object;
if (objLeft == null)
{
if (objRight == null)
return true;
else
return false;
}

return lhs.Equals(rhs);
}

public override bool Equals(object obj)
{
AlarmProfile rhs = (AlarmProfile)obj;
if (rhs == null)
return false;

return this.ProfileId == rhs.ProfileId;
}


So I'm cheking for null all over the place, it this correct design?

thanks in advance
Brian.
 
C

cody

You should do it like this: call equals method from comparison operator not
the other way around:

public override bool Equals(object obj)
{
return obj!=null && ((ZephirDate)obj).Date==this.Date;
}

public static bool operator==(ZephirDate d1, ZephirDate d2)
{
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);
else
return true;
}
 
H

Helge Jensen

cody said:
You should do it like this: call equals method from comparison operator not
the other way around:

My usual idiom:

public override bool Equals(Object obj) { return Equals(obj as Foo); }
public virtual bool Equals(Foo other) {
// leave "==this" test out if property comparisons are cheap
if ( other == this )
return true;
else if ( other == null )
return false;
else if ( other.property != this.property )
return false;
// any number of property tests go here...
else
return true;
}
public static bool Operator==(Foo foo1, Foo foo2) {
if ( ((object)foo1) == null )
return ((object)foo2) == null;
else
return foo1.Equals(foo2);
}
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);

this.Equals(Object o) should never return true if o == null, should it?
 
C

cody

this.Equals(Object o) should never return true if o == null, should it?

Sure it shouldn't. Why do you ask? Thats why I have
"return obj!=null && ((ZephirDate)obj).Date==this.Date;"
in my equals method.
 
H

Helge Jensen

Sure it shouldn't. Why do you ask? Thats why I have
"return obj!=null && ((ZephirDate)obj).Date==this.Date;"
in my equals method.

It's not that it's a big deal,... I ask Because of the code i quoted
above the question, in the "public static bool Operator==":
if (((object)d1)!=null)
return d1.Equals(d2);
else if (((object)d2)!=null)
return d2.Equals(d1);
[... cutted rest ...]

Which actually invokes d2.Equals(null). It runs if, and only if, d1 ==
null && d2 != null. I usually write:

if ( ((object)foo1) == null )
return ((object)foo2) == null;
else
return foo1.Equals(foo2);

instead.

Your code uses right-hand-evaluation of ==, if and only if left-hand
side is null, that's what was a bit wierd to me. My code does left-hand
evaluation always, which is clearer (to me, at least :), and can be done
if "this.Equals(null) != true" is guaranteed.
 

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