In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the analysis of traps that may be caused by the overloading of equal operators in C#". In the daily operation, I believe that many people have doubts about the problems of analyzing traps that may be caused by the overloading of equal operators in C#. The editor consulted all kinds of data and sorted out a simple and useful method of operation. I hope it will be helpful to answer the doubts of "trap analysis that may be caused by the overloading of equal operators in C#". Next, please follow the editor to study!
Recently, I encountered a problem of equality operator overloading when programming, which should be a trap for C #.
The Coordinate class I defined used to overload the equality operator like this:
Publice class Coordinates {.... Public override bool Equals (object obj) {if (! (obj is Coordinates)) return false; Coordinates other = (Coordinates) obj; return (this.longitude.CompareTo (other.longitude) = = 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 also a common situation when operator overloading, but there is a problem when you use it: when a Coordinate object itself is NULL and it is compared with NULL, it looks like this:
Coordinates actualPos = null; if (actualPos = = null) {. } else {. }
The runtime throws an error indicating that a pointer is empty. The result of the trace is that there is a problem with the hosted "= =" operator, which invokes the "lhs.Equals (rhs)" statement, resulting in the absence of lhs itself causing the exception.
For this reason, I tried to rule this out before calling the statement, so I changed the overloaded function to:
Public static bool operator = (Coordinates lhs, Coordinates rhs) {if (lhs = = null) return (rhs = = null); return lhs.Equals (rhs);}
It turns out that the function continues to call itself, followed by an exception.
To solve this problem, you have to break the dead cycle, so try to map lhs to object, as follows:
Public static bool operator = = (Coordinates lhs, Coordinates rhs) {if ((lhs as object) = = null) return ((rhs as object) = = null); return lhs.Equals (rhs);}
When lhs is mapped to object, the "= =" takes the equality operator of object, resulting in a natural OK.
At this point, the study on "the analysis of traps that may be caused by the overloading of equal operators in C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.