Re: implicitly handle operator == when class overrides Equals method

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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;
}

.



Relevant Pages

  • Re: JDBC compare the content of two tables
    ... I want to use getObject method to get the value of each column, ... then use equals method of java.lang.Object to compare two columns' values. ... representation for NaN or not. ...
    (comp.lang.java.help)
  • Re: Newbie question about object comparisons
    ... > However, in the business problem I'm trying to solve, the 'keys' are ... In that case I amend my recommended solution: use the Equals method. ... the correct Equals to call to compare your two keys. ...
    (microsoft.public.dotnet.csharp.general)
  • Re: Sorting arrays (again)
    ... It just tells you that though the compilation went through, ... javac command to uncheck -Xlint? ... To what am I supposed to compare? ... And now I can implement the equals method as well. ...
    (comp.lang.java.help)
  • Re: flyweight object equivalence
    ... I'm running up against a subtlety, namely I am not sure how to provide ... the functionality for testing equality. ... If I wanted to compare this information in C++, ... you would have to provide an Equals method on one of the interfaces they ...
    (microsoft.public.vc.atl)
  • Re: conditional to connect to db
    ... So when you use == it is comparing the values of the objects, which will differ because the getHost String object is not the localhost string object. ... The equals method is overridden in the String class so it compares the actual string of characters, rather than just using == to compare the objects. ... All objects inherit the equals method and you override it depending on how you want to define equality. ...
    (comp.lang.java.help)