Skip to content

Commit 634b360

Browse files
authored
Guid F# snippets (#7729)
1 parent 86eec38 commit 634b360

File tree

20 files changed

+401
-0
lines changed

20 files changed

+401
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module ctor1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let guidStrings =
7+
[ "ca761232ed4211cebacd00aa0057b223"
8+
"CA761232-ED42-11CE-BACD-00AA0057B223"
9+
"{CA761232-ED42-11CE-BACD-00AA0057B223}"
10+
"(CA761232-ED42-11CE-BACD-00AA0057B223)"
11+
"{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" ]
12+
13+
for guidString in guidStrings do
14+
let guid = Guid guidString
15+
printfn $"Original string: {guidString}"
16+
printfn $"Guid: {guid}\n"
17+
18+
// The example displays the following output:
19+
// Original string: ca761232ed4211cebacd00aa0057b223
20+
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
21+
//
22+
// Original string: CA761232-ED42-11CE-BACD-00AA0057B223
23+
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
24+
//
25+
// Original string: {CA761232-ED42-11CE-BACD-00AA0057B223}
26+
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
27+
//
28+
// Original string: (CA761232-ED42-11CE-BACD-00AA0057B223)
29+
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
30+
//
31+
// Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}
32+
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
33+
// </Snippet1>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module ctor2
2+
3+
// <Snippet2>
4+
open System
5+
6+
let g = Guid(0xA, 0xBs, 0xCs, [| 0uy..7uy |])
7+
printfn $"{g:B}"
8+
9+
// The example displays the following output:
10+
// {0000000a-000b-000c-0001-020304050607}
11+
// </Snippet2>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="ctor1.fs" />
9+
<Compile Include="ctor2.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module compareto1
2+
3+
// <Snippet2>
4+
open System
5+
open System.Runtime.InteropServices
6+
7+
[<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")>]
8+
type Example = class end
9+
10+
let guidAttr =
11+
Attribute.GetCustomAttribute(typeof<Example>, typeof<GuidAttribute>) :?> GuidAttribute
12+
13+
let guidValue =
14+
Guid.Parse guidAttr.Value
15+
16+
let values: obj[] =
17+
[| null; 16
18+
Guid.Parse "01e75c83-c6f5-4192-b57e-7427cec5560d"
19+
guidValue |]
20+
21+
for value in values do
22+
try
23+
printfn $"{guidValue} and %A{value}: {guidValue.CompareTo value}"
24+
with :? ArgumentException ->
25+
printfn $"Cannot compare {guidValue} and %A{value}"
26+
27+
// The example displays the following output:
28+
// 936da01f-9abd-4d9d-80c7-02af85c822a8 and <null>: 1
29+
// Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
30+
// 936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
31+
// 936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0
32+
// </Snippet2>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module compareto2
2+
3+
// <Snippet1>
4+
open System
5+
6+
type Comparison =
7+
| ``Less Than`` = -1
8+
| Equals = 0
9+
| ``Greater Than`` = 1
10+
11+
let mainGuid =
12+
Guid.Parse "01e75c83-c6f5-4192-b57e-7427cec5560d"
13+
14+
let guid2 = Guid(0x01e75c83, 0xc6f5s, 0x4192s, [| 0xb5uy; 0x7euy; 0x74uy; 0x27uy; 0xceuy; 0xc5uy; 0x56uy; 0x0cuy |])
15+
let guid3 =
16+
Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d")
17+
18+
printfn $"{mainGuid} {mainGuid.CompareTo guid2 |> enum<Comparison> :F} {guid2}"
19+
printfn $"{mainGuid} {mainGuid.CompareTo guid3 |> enum<Comparison> :F} {guid3}"
20+
21+
// The example displays the following output:
22+
// 01e75c83-c6f5-4192-b57e-7427cec5560d Greater Than 01e75c83-c6f5-4192-b57e-7427cec5560c
23+
// 01e75c83-c6f5-4192-b57e-7427cec5560d Less Than 01e75c84-c6f5-4192-b57e-7427cec5560d
24+
// </Snippet1>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="compareto1.fs" />
9+
<Compile Include="compareto2.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// <Snippet1>
2+
open System
3+
4+
// Create a GUID and determine whether it consists of all zeros.
5+
let guid1 = Guid.NewGuid()
6+
printfn $"{guid1}"
7+
printfn $"Empty: {guid1 = Guid.Empty}\n"
8+
9+
// Create a GUID with all zeros and compare it to Empty.
10+
let bytes = Array.zeroCreate<byte> 16
11+
let guid2 = Guid bytes
12+
printfn $"{guid2}"
13+
printfn $"Empty: {guid2 = Guid.Empty}"
14+
15+
// The example displays output like the following:
16+
// 11c43ee8-b9d3-4e51-b73f-bd9dda66e29c
17+
// Empty: False
18+
//
19+
// 00000000-0000-0000-0000-000000000000
20+
// Empty: True
21+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="empty.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="ng.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//<snippet1>
2+
open System
3+
4+
// Create and display the value of two GUIDs.
5+
let g = Guid.NewGuid()
6+
printfn $"{g}"
7+
printfn $"{Guid.NewGuid()}"
8+
9+
// This code example produces a result similar to the following:
10+
// 0f8fad5b-d9cb-469f-a165-70867728950e
11+
// 7c9e6679-7425-40de-944b-e07fc1f90ae7
12+
//</snippet1>

0 commit comments

Comments
 (0)