1+ //<snippet1>
2+ // This code example demonstrates the
3+ // Nullable<T>.GetValueOrDefault methods.
4+ open System
5+
6+ // Display the values of two nullable of System.Single structures.
7+ // The printfn string interpolation automatically calls the ToString methods of
8+ // each input argument to display its values. If no value is defined for a
9+ // nullable type, the ToString method for that argument returns the empty
10+ // string ("").
11+ let display title dspMySingle dspYourSingle =
12+ printfn $" {title}) mySingle = [{dspMySingle}], yourSingle = [{dspYourSingle}]"
13+
14+ let mySingle = Nullable 12.34 f
15+ let yourSingle = Nullable - 1 f
16+
17+ [<EntryPoint>]
18+ let main _ =
19+ printfn " *** Display a value or the default value ***\n "
20+ // Display the values of mySingle and yourSingle.
21+
22+ display " A1" mySingle yourSingle
23+
24+ // Shadow the value of mySingle to yourSingle, then display the values
25+ // of mySingle and yourSingle. The yourSingle variable is assigned the
26+ // value 12.34 because mySingle has a value.
27+
28+ let yourSingle = mySingle.GetValueOrDefault()
29+ display " A2" mySingle yourSingle
30+
31+ // Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
32+ // defined for mySingle. Then assign the value of mySingle to yourSingle and
33+ // display the values of both variables. The default value of all binary zeroes
34+ // is assigned to yourSingle because mySingle has no value.
35+
36+ let mySingle = Nullable()
37+ let yourSingle = mySingle.GetValueOrDefault()
38+ display " A3" mySingle yourSingle
39+
40+ // Shadow the original values of mySingle and yourSingle.
41+ let mySingle = Nullable 12.34 f
42+ let yourSingle = Nullable - 1.0 f
43+
44+ printf " \n *** Display a value or the "
45+ printfn " specified default value ***\n "
46+
47+ // Display the values of mySingle and yourSingle.
48+ display " B1" mySingle yourSingle
49+
50+ // Shadow the value of mySingle to yourSingle, then display the values
51+ // of mySingle and yourSingle. The yourSingle variable is assigned the
52+ // value 12.34 because mySingle has a value.
53+
54+ let yourSingle = mySingle.GetValueOrDefault - 222.22 f
55+ display " B2" mySingle yourSingle
56+
57+ // Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
58+ // defined for mySingle. Then shadow the value of mySingle to yourSingle and
59+ // display the values of both variables. The specified default value of -333.33
60+ // is assigned to yourSingle because mySingle has no value.
61+
62+ let mySingle = Nullable()
63+ let yourSingle = mySingle.GetValueOrDefault - 333.33 f
64+ display " B3" mySingle yourSingle
65+ 0
66+
67+ // This code example produces the following results:
68+ // A1) mySingle = [12.34], yourSingle = [-1]
69+ // A2) mySingle = [12.34], yourSingle = [12.34]
70+ // A3) mySingle = [], yourSingle = [0]
71+ //
72+ // *** Display a value or the specified default value ***
73+ //
74+ // B1) mySingle = [12.34], yourSingle = [-1]
75+ // B2) mySingle = [12.34], yourSingle = [12.34]
76+ // B3) mySingle = [], yourSingle = [-333.33]
77+ //</snippet1>
0 commit comments