C # traps and solutions for reloading equal Operators

Source: Internet
Author: User

Recently, I encountered an equal operator overload problem during programming. This is a trap of C.

The Coordinate class I defined originally used to overload the equality operator as follows:

Publice class Coordinates

{

....

Public override bool Equals (object obj)
{
If (! (Obj is Coordinates) return false;

Coordinates other = (Coordinates) obj;

Return (this. longwheel. CompareTo (other. longwheel) = 0) & (this. latitude. CompareTo (other. latitude) = 0 );
}

Public static bool operator = (Coordinates lhs, Coordinates rhs)
{
Return lhs. Equals (rhs );
}

Public static bool operator! = (Coordinates lhs, Coordinates rhs)
{
Return! (Lhs = rhs );
}

...

}

This is often seen in the case of operator overloading, but there are some problems when using it: that is, when a Coordinate object is NULL and it is compared with NULL, It is shown as follows:

Coordinates actualPos = null;

If (actualPos = null)
{... }
Else
{... }
An error is thrown during runtime, prompting that a pointer is null. The trace result shows that there is a problem with the "=" Operator. It calls the "lhs. Equals (rhs)" Statement and the result is that the lhs itself does not exist, causing an exception.

Therefore, I tried to exclude this situation before calling this statement, so I changed the overload function:

Public static bool operator = (Coordinates lhs, Coordinates rhs)
{
If (lhs = null) return (rhs = null );

Return lhs. Equals (rhs );
}

The result shows that this function will continue to call itself, and then the exception still occurs.

 

To solve this problem, we must break this endless loop and try to map lhs to object, as shown below:

Public static bool operator = (Coordinates lhs, Coordinates rhs)
{
If (lhs as object) = null) return (rhs as object) = null );

Return lhs. Equals (rhs );
}

After the lhs is mapped to "=" after the object, the equal operator of the object is used, and the result is naturally OK. I believe that the use of C # programming will sooner or later, and it may be inexplicable. I hope the above articles will be useful to you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.