. NET overloaded operators

Source: Internet
Author: User

The code is as follows:

1  /// <summary>2     ///coordinates (structure type)3     /// </summary>4      Public structcoordinate5     {6          Public intx;7          Public inty;8 9          PublicCoordinate (intXinty)Ten         { One              This. x =x; A              This. y =y; -         } -  the         /// <summary> -         ///object exists in the collection -         /// </summary> -         /// <param name= "list" ></param> +         /// <returns></returns> -          Public BOOLIsoneof (list<coordinate>list) +         { A             if(list. IndexOf ( This) >=0) at             { -                 return true; -             } -             return false; -         } -  in         /// <summary> -         ///comparison of two coordinates equal to         /// </summary> +         /// <param name= "C" ></param> -         /// <returns></returns> the          Public Override BOOLEquals (Objectc) *         { $             if(C.gettype ()! = This. GetType ())Panax Notoginseng                 return false; -  thecoordinate coordinate =(coordinate) C; +  A             if( This. x! =coordinate.x) the                 return false; +  -             if( This. Y! =coordinate.y) $                 return false; $  -             return true; -         } the  -         /// <summary>Wuyi         ///comparison of two coordinates equal the         /// </summary> -         /// <param name= "LHS" ></param> Wu         /// <param name= "RHS" ></param> -         /// <returns></returns> About          Public Static BOOL operator==(coordinate lhs, coordinate RHS) $         { -             returnLHS. Equals (RHS); -         } -  A         /// <summary> +         ///comparison of two coordinates is not equal the         /// </summary> -         /// <param name= "LHS" ></param> $         /// <param name= "RHS" ></param> the         /// <returns></returns> the          Public Static BOOL operator!=(coordinate lhs, coordinate RHS) the         { the             return!LHS. Equals (RHS); -         } in  the         /// <summary> the         ///add two coordinates to get new coordinates About         /// </summary> the         /// <param name= "LHS" ></param> the         /// <param name= "RHS" ></param> the         /// <returns></returns> +          Public StaticCoordinateoperator+(coordinate lhs, coordinate RHS) -         { the             return Newcoordinate ()Bayi             { thex = lhs.x +Rhs.x, they = lhs.y +Rhs.y -             }; -         } the  the         /// <summary> the         ///two coordinate image minus to get new coordinates the         /// </summary> -         /// <param name= "LHS" ></param> the         /// <param name= "RHS" ></param> the         /// <returns></returns> the          Public StaticCoordinateoperator-(coordinate lhs, coordinate RHS)94         { the             return Newcoordinate () the             { thex = lhs.x-Rhs.x,98y = lhs.y-Rhs.y About             }; -         }101 102          Public Override intGetHashCode ()103         {104             return Base. GetHashCode (); the         }106}
View Code

Use the following

            Coordinate coor1 = new coordinate (ten);            Coordinate coor2 = new coordinate (+);            Console.WriteLine ("Coor1 = = coor2:{0}", Coor1 = = Coor2);            var Coor3 = Coor2-coor1;            Console.WriteLine ("Coor2-coor1 obtains new coordinates" x:{0} "" Y:{1} "", coor3.x, coor3.y);            Coor3 = Coor2 + Coor1;            Console.WriteLine ("Coor2 + coor1 get new coordinates" x:{0} "" Y:{1} "", coor3.x, COOR3.Y);

Operation Result:

. NET overloaded operator

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.