Simple Example of Operator Overloading: Version size comparison

Source: Internet
Author: User
Namespace test {using system; using microshaoft; public class class1 {static void main (string [] ARGs) {console. writeline ("first \". \ "and the second \". \ "Comparison of numbers by decimal places, comparison of other numbers"); stringversion V1 = new stringversion ("12.0390.001"); stringversion v2 = new stringversion ("21.039000.1"); console. writeline ("version: [{0}] [{1}]", v1.tostring (), v2.tostring (); console. writeline ("gethashcode: [{0}] [{1}]", v1.gethashcode (), V2.gethashcode (); console. writeline ("equals, {0}", v1.equals (V2); console. writeline ("=, {0}", V1 = V2); console. writeline ("! =, {0} ", V1! = V2); console. writeline (">,{ 0}", V1> V2); console. writeline ("<, {0}", V1 <V2); console. writeline (">=, {0}", V1> = V2); console. writeline ("<=, {0}", V1 <= V2); console. writeline (environment. version. tostring () ;}} namespace microshaoft {using system; public class stringversion {private string [] _ versions; private string _ version; protected string [] versions {get {return _ versions ;}} Public stringversion (string version) {_ version = version; char [] C = new char [] {'. '}; _ versions = version. split (c);} public override string tostring () {return _ version;} public override bool equals (Object O) {stringversion x = This; stringversion y = O as stringversion; int XL = x. versions. length; int yl = y. versions. length; For (INT I = 0; I <XL | I <yl; I ++) {uint xi = 0; uint YI = 0; Double XD = 0.0d; double YD = 0.0d; if (I <XL) {if (I! = 1) {uint. tryparse (X. versions [I], out XI);} else {double. tryparse (string. format ("0. {0} ", X. versions [I]), Out xd) ;}} if (I <yl) {if (I! = 1) {uint. tryparse (Y. versions [I], out Yi);} else {double. tryparse (string. format ("0. {0} ", Y. versions [I]), Out YD) ;}} if (I = 1) {If (XD! = YD) {return false ;}} else {If (Xi! = Yi) {return false ;}}return true;} public override int gethashcode () {string [] A = new string [_ versions. length]; array. copy (_ versions, 0, A, 0,. length); For (INT I = 0; I <_ versions. length; I ++) {if (I = 1) {A [I] = _ versions [I]. trimend ('0');} else {A [I] = _ versions [I]. trimstart ('0') ;}} string S = string. join (". ", a); Return S. gethashcode ();} public static bool operator = (strin Gversion X, stringversion y) {return X. Equals (y);} public static bool Operator! = (Stringversion X, stringversion y) {int XL = x. versions. length; int yl = y. versions. length; For (INT I = 0; I <XL | I <yl; I ++) {uint xi = 0; uint YI = 0; double XD = 0.0d; double YD = 0.0d; if (I <XL) {if (I! = 1) {uint. tryparse (X. versions [I], out XI);} else {double. tryparse (string. format ("0. {0} ", X. versions [I]), Out xd) ;}} if (I <yl) {if (I! = 1) {uint. tryparse (Y. versions [I], out Yi);} else {double. tryparse (string. format ("0. {0} ", Y. versions [I]), Out YD) ;}} if (I = 1) {If (XD! = YD) {return true ;}} else {If (Xi! = Yi) {return true ;}}return false;} public static bool operator> (stringversion X, stringversion y) {int XL = x. versions. length; int yl = y. versions. length; For (INT I = 0; I <XL | I <yl; I ++) {uint xi = 0; uint YI = 0; double XD = 0.0d; double YD = 0.0d; if (I <XL) {if (I! = 1) {uint. tryparse (X. versions [I], out XI);} else {double. tryparse (string. format ("0. {0} ", X. versions [I]), Out xd) ;}} if (I <yl) {if (I! = 1) {uint. tryparse (Y. versions [I], out Yi);} else {double. tryparse (string. format ("0. {0} ", Y. versions [I]), Out YD) ;}if (I = 1) {If (XD> YD) {return true ;} else if (XD <YD) {return false ;}} else {If (xi> Yi) {return true;} else if (xi <Yi) {return false ;}} return false ;} public static bool operator <(stringversion X, stringversion y) {int XL = x. versions. length; int y L = y. versions. length; For (INT I = 0; I <XL | I <yl; I ++) {uint xi = 0; uint YI = 0; double XD = 0.0d; double YD = 0.0d; if (I <XL) {if (I! = 1) {uint. tryparse (X. versions [I], out XI);} else {double. tryparse (string. format ("0. {0} ", X. versions [I]), Out xd) ;}} if (I <yl) {if (I! = 1) {uint. tryparse (Y. versions [I], out Yi);} else {double. tryparse (string. format ("0. {0} ", Y. versions [I]), Out YD) ;}if (I = 1) {If (XD <YD) {return true ;} else if (XD> YD) {return false ;}} else {If (xi <Yi) {return true;} else if (xi> Yi) {return false ;}} return false ;} public static bool operator> = (stringversion X, stringversion y) {int XL = x. versions. length; int YL = y. versions. length; bool equal = true; For (INT I = 0; I <XL | I <yl; I ++) {uint xi = 0; uint YI = 0; double XD = 0.0d; double YD = 0.0d; if (I <XL) {if (I! = 1) {uint. tryparse (X. versions [I], out XI);} else {double. tryparse (string. format ("0. {0} ", X. versions [I]), Out xd) ;}} if (I <yl) {if (I! = 1) {uint. tryparse (Y. versions [I], out Yi);} else {double. tryparse (string. format ("0. {0} ", Y. versions [I]), Out YD) ;}if (I = 1) {If (XD> YD) {return true ;} else if (XD <YD) {return false;} equal = equal & (XD = YD);} else {If (xi> Yi) {return true;} else if (xi <Yi) {return false;} equal = equal & (xi = Yi) ;}} return equal;} public static bool operator <= (stringvers Ion X, stringversion y) {int XL = x. versions. length; int yl = y. versions. length; bool equal = true; For (INT I = 0; I <XL | I <yl; I ++) {uint xi = 0; uint YI = 0; double XD = 0.0d; double YD = 0.0d; if (I <XL) {if (I! = 1) {uint. tryparse (X. versions [I], out XI);} else {double. tryparse (string. format ("0. {0} ", X. versions [I]), Out xd) ;}} if (I <yl) {if (I! = 1) {uint. tryparse (Y. versions [I], out Yi);} else {double. tryparse (string. format ("0. {0} ", Y. versions [I]), Out YD) ;}if (I = 1) {If (XD <YD) {return true ;} else if (XD> YD) {return false;} equal = equal & (XD = YD);} else {If (xi <Yi) {return true;} else if (xi> Yi) {return false;} equal = equal & (xi = Yi) ;}} return equal ;}}}

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.