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