1+ //<snippet1>
2+ // This example demonstrates the generic and non-generic versions of the
3+ // CompareTo method for several base types.
4+ // The non-generic version takes a parameter of type Object, while the generic
5+ // version takes a type-specific parameter, such as Boolean, Int32, or Double.
6+ open System
7+
8+ let show caption ( var1 : obj ) ( var2 : obj ) resultGeneric resultNonGeneric =
9+ printf " %s " caption
10+ if resultGeneric = resultNonGeneric then
11+ let relation =
12+ if resultGeneric < 0 then " less than"
13+ elif resultGeneric > 0 then " greater than"
14+ else " equal to"
15+ printfn $" {var1} is {relation} {var2}"
16+
17+ // The following condition will never occur because the generic and non-generic
18+ // CompareTo methods are equivalent.
19+ else
20+ printfn $" Generic CompareTo = {resultGeneric} non-generic CompareTo = {resultNonGeneric}"
21+
22+ let now = DateTime.Now
23+ // Time span = 11 days, 22 hours, 33 minutes, 44 seconds
24+ let tsX = TimeSpan( 11 , 22 , 33 , 44 )
25+ // Version = 1.2.333.4
26+ let versX = Version " 1.2.333.4"
27+ // Guid = CA761232-ED42-11CE-BACD-00AA0057B223
28+ let guidX = Guid " {CA761232-ED42-11CE-BACD-00AA0057B223}"
29+
30+ let a1 , a2 = true , true
31+ let b1 , b2 = 1 uy, 1 uy
32+ let c1 , c2 = - 2 s, 2 s
33+ let d1 , d2 = 3 , 3
34+ let e1 , e2 = 4 L, - 4 L
35+ let f1 , f2 = - 5.5 m, 5.5 m
36+ let g1 , g2 = 6.6 f, 6.6 f
37+ let h1 , h2 = 7.7 , - 7.7
38+ let i1 , i2 = 'A' , 'A'
39+ let j1 , j2 = " abc" , " abc"
40+ let k1 , k2 = now, now
41+ let l1 , l2 = tsX, tsX
42+ let m1 , m2 = versX, Version " 2.0"
43+ let n1 , n2 = guidX, guidX
44+
45+ // The following types are not CLS-compliant.
46+ let w1 , w2 = 8 y, 8 y
47+ let x1 , x2 = 9 us, 9 us
48+ let y1 , y2 = 10 u, 10 u
49+ let z1 , z2 = 11 uL, 11 uL
50+
51+ printfn " \n The following is the result of using the generic and non-generic\n versions of the CompareTo method for several base types:\n "
52+ try
53+ // The second and third show function call parameters are automatically boxed because
54+ // the second and third show function declaration arguments expect type Object.
55+ show " Boolean: " a1 a2 ( a1.CompareTo a2) ( a1.CompareTo ( a2 :> obj))
56+
57+ show " Byte: " b1 b2 ( b1.CompareTo b2) ( b1.CompareTo ( b2 :> obj))
58+ show " Int16: " c1 c2 ( c1.CompareTo c2) ( c1.CompareTo ( c2 :> obj))
59+ show " Int32: " d1 d2 ( d1.CompareTo d2) ( d1.CompareTo ( d2 :> obj))
60+ show " Int64: " e1 e2 ( e1.CompareTo e2) ( e1.CompareTo ( e2 :> obj))
61+ show " Decimal: " f1 f2 ( f1.CompareTo f2) ( f1.CompareTo ( f2 :> obj))
62+ show " Single: " g1 g2 ( g1.CompareTo g2) ( g1.CompareTo ( g2 :> obj))
63+ show " Double: " h1 h2 ( h1.CompareTo h2) ( h1.CompareTo ( h2 :> obj))
64+ show " Char: " i1 i2 ( i1.CompareTo i2) ( i1.CompareTo ( i2 :> obj))
65+ show " String: " j1 j2 ( j1.CompareTo j2) ( j1.CompareTo ( j2 :> obj))
66+ show " DateTime: " k1 k2 ( k1.CompareTo k2) ( k1.CompareTo ( k2 :> obj))
67+ show " TimeSpan: " l1 l2 ( l1.CompareTo l2) ( l1.CompareTo ( l2 :> obj))
68+ show " Version: " m1 m2 ( m1.CompareTo m2) ( m1.CompareTo ( m2 :> obj))
69+ show " Guid: " n1 n2 ( n1.CompareTo n2) ( n1.CompareTo ( n2 :> obj))
70+
71+ printfn " \n The following types are not CLS-compliant:"
72+ show " SByte: " w1 w2 ( w1.CompareTo w2) ( w1.CompareTo ( w2 :> obj))
73+ show " UInt16: " x1 x2 ( x1.CompareTo x2) ( x1.CompareTo ( x2 :> obj))
74+ show " UInt32: " y1 y2 ( y1.CompareTo y2) ( y1.CompareTo ( y2 :> obj))
75+ show " UInt64: " z1 z2 ( z1.CompareTo z2) ( z1.CompareTo ( z2 :> obj))
76+ with e -> printfn $" {e}"
77+
78+
79+ // This example produces the following results:
80+ // The following is the result of using the generic and non-generic versions of the
81+ // CompareTo method for several base types:
82+ // Boolean: True is equal to True
83+ // Byte: 1 is equal to 1
84+ // Int16: -2 is less than 2
85+ // Int32: 3 is equal to 3
86+ // Int64: 4 is greater than -4
87+ // Decimal: -5.5 is less than 5.5
88+ // Single: 6.6 is equal to 6.6
89+ // Double: 7.7 is greater than -7.7
90+ // Char: A is equal to A
91+ // String: abc is equal to abc
92+ // DateTime: 12/1/2003 5:37:46 PM is equal to 12/1/2003 5:37:46 PM
93+ // TimeSpan: 11.22:33:44 is equal to 11.22:33:44
94+ // Version: 1.2.333.4 is less than 2.0
95+ // Guid: ca761232-ed42-11ce-bacd-00aa0057b223 is equal to ca761232-ed42-11ce-bacd-00
96+ // aa0057b223
97+ // The following types are not CLS-compliant:
98+ // SByte: 8 is equal to 8
99+ // UInt16: 9 is equal to 9
100+ // UInt32: 10 is equal to 10
101+ // UInt64: 11 is equal to 11
102+ //</snippet1>
0 commit comments