Re: implicitly handle operator == when class overrides Equals method
- From: Steve Richter <StephenRichter@xxxxxxxxx>
- Date: Mon, 18 Feb 2008 12:14:16 -0800 (PST)
On Feb 18, 2:53 pm, Jon Skeet [C# MVP] <sk...@xxxxxxxxx> wrote:
Steve Richter <StephenRich...@xxxxxxxxx> wrote:
Can I get the C# compiler to implicity implement the relational
operators when my class overrides the Equals method and implements the
IComparer interface?
No. You have to implement it yourself.
coding operator== is a bit of work since I have to first cast to
object and compare for null. Then compare for equality. ( is that
true? )
Why would you cast to object?
the operator== method was being recursively called when I compared
factor1 to null.
public static bool operator ==(DisplayLocation InFac1,
DisplayLocation InFac2)
{
object obj1 = (object)InFac1;
object obj2 = (object)InFac2;
if ((obj1 == null) && (obj2 == null))
return true;
else if (obj1 == null)
return false;
else if (obj2 == null)
return false;
else if ((InFac1.Row == InFac2.Row) &&
(InFac1.Column == InFac2.Column))
return true;
else
return false;
}
.
- Follow-Ups:
- Re: implicitly handle operator == when class overrides Equals method
- From: Jon Skeet [C# MVP]
- Re: implicitly handle operator == when class overrides Equals method
- References:
- implicitly handle operator == when class overrides Equals method
- From: Steve Richter
- Re: implicitly handle operator == when class overrides Equals method
- From: Jon Skeet [C# MVP]
- implicitly handle operator == when class overrides Equals method
- Prev by Date: SQL EFFICIENCY: using directive vs try/catch
- Next by Date: Re: SQL EFFICIENCY: using directive vs try/catch
- Previous by thread: Re: implicitly handle operator == when class overrides Equals method
- Next by thread: Re: implicitly handle operator == when class overrides Equals method
- Index(es):
Relevant Pages
|