Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Nullable F# snippets #7795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Nullable/GetUnderlyingType/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="gut.fs" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/Nullable/GetUnderlyingType/gut.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//<snippet1>
// This code example demonstrates the
// Nullable.GetUnderlyingType() method.
open System

// Declare a type named Example.
// The MyMethod member of Example returns a Nullable of Int32.

type Example() =
member _.MyMethod() =
Nullable 0

(*
Use reflection to obtain a Type object for the Example type.
Use the Type object to obtain a MethodInfo object for the MyMethod method.
Use the MethodInfo object to obtain the type of the return value of
MyMethod, which is Nullable of Int32.
Use the GetUnderlyingType method to obtain the type argument of the
return value type, which is Int32.
*)
let t = typeof<Example>
let mi = t.GetMethod "MyMethod"
let retval = mi.ReturnType
printfn $"Return value type ... {retval}"
let answer = Nullable.GetUnderlyingType retval
printfn $"Underlying type ..... {answer}"

// This code example produces the following results:
// Return value type ... System.Nullable`1[System.Int32]
// Underlying type ..... System.Int32
//</snippet1>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/NullableT/Equals/eq.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//<snippet1>
// This code example demonstrates the Nullable<T>.Equals
// methods.
open System

let nullInt1 = Nullable 100
let nullInt2 = Nullable 200

// Determine if two nullable of System.Int32 values are equal.
// The nullable objects have different values.
printf "1) nullInt1 and nullInt2 "
if nullInt1.Equals nullInt1 then
printf "are"
else
printf "are not"
printfn " equal."

// Determine if a nullable of System.Int32 and an object
// are equal. The object contains the boxed value of the
// nullable object.

let myObj = box nullInt1
printf "2) nullInt1 and myObj "
if nullInt1.Equals myObj then
printf "are"
else
printf "are not"
printfn " equal."

// This code example produces the following results:
// 1) nullInt1 and nullInt2 are not equal.
// 2) nullInt1 and myObj are equal.
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/NullableT/Equals/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="eq.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/NullableT/GetValueOrDefault/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="gvod.fs" />
</ItemGroup>
</Project>
77 changes: 77 additions & 0 deletions snippets/fsharp/System/NullableT/GetValueOrDefault/gvod.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//<snippet1>
// This code example demonstrates the
// Nullable<T>.GetValueOrDefault methods.
open System

// Display the values of two nullable of System.Single structures.
// The printfn string interpolation automatically calls the ToString methods of
// each input argument to display its values. If no value is defined for a
// nullable type, the ToString method for that argument returns the empty
// string ("").
let display title dspMySingle dspYourSingle =
printfn $"{title}) mySingle = [{dspMySingle}], yourSingle = [{dspYourSingle}]"

let mySingle = Nullable 12.34f
let yourSingle = Nullable -1f

[<EntryPoint>]
let main _ =
printfn "*** Display a value or the default value ***\n"
// Display the values of mySingle and yourSingle.

display "A1" mySingle yourSingle

// Shadow the value of mySingle to yourSingle, then display the values
// of mySingle and yourSingle. The yourSingle variable is assigned the
// value 12.34 because mySingle has a value.

let yourSingle = mySingle.GetValueOrDefault()
display "A2" mySingle yourSingle

// Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
// defined for mySingle. Then assign the value of mySingle to yourSingle and
// display the values of both variables. The default value of all binary zeroes
// is assigned to yourSingle because mySingle has no value.

let mySingle = Nullable()
let yourSingle = mySingle.GetValueOrDefault()
display "A3" mySingle yourSingle

// Shadow the original values of mySingle and yourSingle.
let mySingle = Nullable 12.34f
let yourSingle = Nullable -1.0f

printf "\n*** Display a value or the "
printfn "specified default value ***\n"

// Display the values of mySingle and yourSingle.
display "B1" mySingle yourSingle

// Shadow the value of mySingle to yourSingle, then display the values
// of mySingle and yourSingle. The yourSingle variable is assigned the
// value 12.34 because mySingle has a value.

let yourSingle = mySingle.GetValueOrDefault -222.22f
display "B2" mySingle yourSingle

// Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
// defined for mySingle. Then shadow the value of mySingle to yourSingle and
// display the values of both variables. The specified default value of -333.33
// is assigned to yourSingle because mySingle has no value.

let mySingle = Nullable()
let yourSingle = mySingle.GetValueOrDefault -333.33f
display "B3" mySingle yourSingle
0

// This code example produces the following results:
// A1) mySingle = [12.34], yourSingle = [-1]
// A2) mySingle = [12.34], yourSingle = [12.34]
// A3) mySingle = [], yourSingle = [0]
//
// *** Display a value or the specified default value ***
//
// B1) mySingle = [12.34], yourSingle = [-1]
// B2) mySingle = [12.34], yourSingle = [12.34]
// B3) mySingle = [], yourSingle = [-333.33]
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/NullableT/HasValue/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="hasvalue2.fs" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/NullableT/HasValue/hasvalue2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <Snippet1>
open System

let n1 = Nullable 10
let n2 = Nullable()
let mutable n3 = Nullable 20
n3 <- Nullable()
let items = [| n1; n2; n3 |]

for item in items do
printfn $"Has a value: {item.HasValue}"
if item.HasValue then
printfn $"Type: {item.GetType().Name}"
printfn $"Value: {item.Value}"
else
printfn $"Null: {item = Nullable()}"
printfn $"Default Value: {item.GetValueOrDefault()}"
printfn ""
// The example displays the following output:
// Has a value: True
// Type: Int32
// Value: 10
//
// Has a value: False
// Null: True
// Default Value: 0
//
// Has a value: False
// Null: True
// Default Value: 0
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/NullableT/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="tarow.fs" />
</ItemGroup>
</Project>
71 changes: 71 additions & 0 deletions snippets/fsharp/System/NullableT/Overview/tarow.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//<snippet1>
open System

// Define the "titleAuthor" table of the Microsoft "pubs" database.

type titleAuthor =
struct
// Author ID format ###-##-####
val mutable au_id: string
// Title ID format AA####
val mutable title_id: string
// Author ORD is nullable.
val mutable au_ord: Nullable<int16>
// Royalty Percent is nullable.
val mutable royaltyper: Nullable<int>
end

// Display the values of the titleAuthor array elements.
let display dspTitle (dspAllTitleAuthors: #seq<titleAuthor>) =
printfn $"*** {dspTitle} ***"
for dspTA in dspAllTitleAuthors do
printfn $"Author ID ... {dspTA.au_id}"
printfn $"Title ID .... {dspTA.title_id}"
printfn $"Author ORD .. {dspTA.au_ord.GetValueOrDefault -1s}"
printfn $"Royalty %% ... {dspTA.royaltyper.GetValueOrDefault -1}\n"

// Declare and initialize the titleAuthor array.
let ta = Array.zeroCreate<titleAuthor> 3
ta[0].au_id <- "712-32-1176"
ta[0].title_id <- "PS3333"
ta[0].au_ord <- Nullable 1s
ta[0].royaltyper <- Nullable 100

ta[1].au_id <- "213-46-8915"
ta[1].title_id <- "BU1032"
ta[1].au_ord <- Nullable()
ta[1].royaltyper <- Nullable()

ta[2].au_id <- "672-71-3249"
ta[2].title_id <- "TC7777"
ta[2].au_ord <- Nullable()
ta[2].royaltyper <- Nullable 40

// Display the values of the titleAuthor array elements, and
// display a legend.
display "Title Authors Table" ta
printfn "Legend:"
printfn "An Author ORD of -1 means no value is defined."
printfn "A Royalty %% of 0 means no value is defined."

// The example displays the following output:
// *** Title Authors Table ***
// Author ID ... 712-32-1176
// Title ID .... PS3333
// Author ORD .. 1
// Royalty % ... 100
//
// Author ID ... 213-46-8915
// Title ID .... BU1032
// Author ORD .. -1
// Royalty % ... 0
//
// Author ID ... 672-71-3249
// Title ID .... TC7777
// Author ORD .. -1
// Royalty % ... 40
//
// Legend:
// An Author ORD of -1 means no value is defined.
// A Royalty % of 0 means no value is defined.
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/NullableT/ToString/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="ts.fs" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/NullableT/ToString/ts.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//<snippet1>
// This code example demonstrates the
// Nullable<T>.ToString method.
open System

// Display the text representation of a nullable DateTime.
let display title dspDT =
let msg = dspDT.ToString()

printf $"{title} "
if String.IsNullOrEmpty msg then
printfn "The nullable DateTime has no defined value."
else
printfn $"The current date and time is {msg}."

[<EntryPoint>]
let main _ =
// Display the current date and time.
let nullableDate = Nullable DateTime.Now
display "1)" nullableDate

// Assign null (Nothing in Visual Basic) to nullableDate, then
// display its value.
let nullableDate = Nullable()
display "2)" nullableDate
0

// This code example produces the following results:
// 1) The current date and time is 4/19/2005 8:28:14 PM.
// 2) The nullable DateTime has no defined value.
//</snippet1>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/NullableT/op_Explicit/explicit1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// <Snippet1>
open System

let nullInt = Nullable 172
// Convert with int conversion function.
printfn $"{int nullInt}"
// Convert with Convert.ChangeType.
printfn $"{Convert.ChangeType(nullInt, typeof<int>)}"
// The example displays the following output:
// 172
// 172
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/NullableT/op_Explicit/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="explicit1.fs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions xml/System/Nullable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
The following code example defines a method whose return value is of type <xref:System.Nullable%601> of <xref:System.Int32>. The code example uses the <xref:System.Nullable.GetUnderlyingType%2A> method to display the type argument of the return value.

:::code language="csharp" source="~/snippets/csharp/System/Nullable/GetUnderlyingType/gut.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/Nullable/GetUnderlyingType/gut.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullable_GetUnderlyingType/vb/gut.vb" id="Snippet1":::

]]></format>
Expand Down
Loading