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

Skip to content

System.Guid F# snippets #7729

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
Feb 17, 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
33 changes: 33 additions & 0 deletions snippets/fsharp/System/Guid/.ctor/ctor1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module ctor1

// <Snippet1>
open System

let guidStrings =
[ "ca761232ed4211cebacd00aa0057b223"
"CA761232-ED42-11CE-BACD-00AA0057B223"
"{CA761232-ED42-11CE-BACD-00AA0057B223}"
"(CA761232-ED42-11CE-BACD-00AA0057B223)"
"{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" ]

for guidString in guidStrings do
let guid = Guid guidString
printfn $"Original string: {guidString}"
printfn $"Guid: {guid}\n"

// The example displays the following output:
// Original string: ca761232ed4211cebacd00aa0057b223
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
//
// Original string: CA761232-ED42-11CE-BACD-00AA0057B223
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
//
// Original string: {CA761232-ED42-11CE-BACD-00AA0057B223}
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
//
// Original string: (CA761232-ED42-11CE-BACD-00AA0057B223)
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
//
// Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}
// Guid: ca761232-ed42-11ce-bacd-00aa0057b223
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/Guid/.ctor/ctor2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ctor2

// <Snippet2>
open System

let g = Guid(0xA, 0xBs, 0xCs, [| 0uy..7uy |])
printfn $"{g:B}"

// The example displays the following output:
// {0000000a-000b-000c-0001-020304050607}
// </Snippet2>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/Guid/.ctor/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="ctor1.fs" />
<Compile Include="ctor2.fs" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions snippets/fsharp/System/Guid/CompareTo/compareto1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module compareto1

// <Snippet2>
open System
open System.Runtime.InteropServices

[<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")>]
type Example = class end

let guidAttr =
Attribute.GetCustomAttribute(typeof<Example>, typeof<GuidAttribute>) :?> GuidAttribute

let guidValue =
Guid.Parse guidAttr.Value

let values: obj[] =
[| null; 16
Guid.Parse "01e75c83-c6f5-4192-b57e-7427cec5560d"
guidValue |]

for value in values do
try
printfn $"{guidValue} and %A{value}: {guidValue.CompareTo value}"
with :? ArgumentException ->
printfn $"Cannot compare {guidValue} and %A{value}"

// The example displays the following output:
// 936da01f-9abd-4d9d-80c7-02af85c822a8 and <null>: 1
// Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
// 936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
// 936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0
// </Snippet2>
24 changes: 24 additions & 0 deletions snippets/fsharp/System/Guid/CompareTo/compareto2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module compareto2

// <Snippet1>
open System

type Comparison =
| ``Less Than`` = -1
| Equals = 0
| ``Greater Than`` = 1

let mainGuid =
Guid.Parse "01e75c83-c6f5-4192-b57e-7427cec5560d"

let guid2 = Guid(0x01e75c83, 0xc6f5s, 0x4192s, [| 0xb5uy; 0x7euy; 0x74uy; 0x27uy; 0xceuy; 0xc5uy; 0x56uy; 0x0cuy |])
let guid3 =
Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d")

printfn $"{mainGuid} {mainGuid.CompareTo guid2 |> enum<Comparison> :F} {guid2}"
printfn $"{mainGuid} {mainGuid.CompareTo guid3 |> enum<Comparison> :F} {guid3}"

// The example displays the following output:
// 01e75c83-c6f5-4192-b57e-7427cec5560d Greater Than 01e75c83-c6f5-4192-b57e-7427cec5560c
// 01e75c83-c6f5-4192-b57e-7427cec5560d Less Than 01e75c84-c6f5-4192-b57e-7427cec5560d
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/Guid/CompareTo/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="compareto1.fs" />
<Compile Include="compareto2.fs" />
</ItemGroup>
</Project>
21 changes: 21 additions & 0 deletions snippets/fsharp/System/Guid/Empty/empty.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <Snippet1>
open System

// Create a GUID and determine whether it consists of all zeros.
let guid1 = Guid.NewGuid()
printfn $"{guid1}"
printfn $"Empty: {guid1 = Guid.Empty}\n"

// Create a GUID with all zeros and compare it to Empty.
let bytes = Array.zeroCreate<byte> 16
let guid2 = Guid bytes
printfn $"{guid2}"
printfn $"Empty: {guid2 = Guid.Empty}"

// The example displays output like the following:
// 11c43ee8-b9d3-4e51-b73f-bd9dda66e29c
// Empty: False
//
// 00000000-0000-0000-0000-000000000000
// Empty: True
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Guid/Empty/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="empty.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Guid/NewGuid/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="ng.fs" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/Guid/NewGuid/ng.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//<snippet1>
open System

// Create and display the value of two GUIDs.
let g = Guid.NewGuid()
printfn $"{g}"
printfn $"{Guid.NewGuid()}"

// This code example produces a result similar to the following:
// 0f8fad5b-d9cb-469f-a165-70867728950e
// 7c9e6679-7425-40de-944b-e07fc1f90ae7
//</snippet1>
41 changes: 41 additions & 0 deletions snippets/fsharp/System/Guid/Overview/Guids.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//<Snippet1>
open System
open System.Runtime.InteropServices

// Guid for the interface IMyInterface.
[<Guid "F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4">]
type IMyInterface =
abstract MyMethod: unit -> unit

// Guid for the coclass MyTestClass.
[<Guid "936DA01F-9ABD-4d9d-80C7-02AF85C822A8">]
type MyTestClass() =
interface IMyInterface with
member _.MyMethod() = ()

let IMyInterfaceAttribute =
Attribute.GetCustomAttribute(typeof<IMyInterface>, typeof<GuidAttribute>) :?> GuidAttribute

printfn $"IMyInterface Attribute: {IMyInterfaceAttribute.Value}"

// Use the string to create a guid.
let myGuid1 = Guid IMyInterfaceAttribute.Value
// Use a byte array to create a guid.
let myGuid2 = Guid(myGuid1.ToByteArray())

if myGuid1.Equals myGuid2 then
printfn "myGuid1 equals myGuid2"
else
printfn "myGuid1 does not equal myGuid2"

// Equality operator can also be used to determine if two guids have same value.
if myGuid1 = myGuid2 then
printfn "myGuid1 == myGuid2"
else
printfn "myGuid1 <> myGuid2"

// The example displays the following output:
// IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
// myGuid1 equals myGuid2
// myGuid1 == myGuid2
//</Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Guid/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="Guids.fs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/Guid/Parse/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="parseex1.fs" />
<Compile Include="parseexactex1.fs" />
<Compile Include="tryparseex1.fs" />
<Compile Include="tryparseexactex1.fs" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions snippets/fsharp/System/Guid/Parse/parseex1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module parseex1

// <Snippet3>
open System

let originalGuid = Guid.NewGuid()
// Create an array of string representations of the GUID.
let stringGuids =
[| originalGuid.ToString "B"
originalGuid.ToString "D"
originalGuid.ToString "X" |]

// Parse each string representation.
for stringGuid in stringGuids do
try
let newGuid = Guid.Parse stringGuid
printfn $"Converted {stringGuid} to a Guid"
with
| :? ArgumentNullException ->
printfn "The string to be parsed is null."
| :? FormatException ->
printfn $"Bad format: {stringGuid}"

// The example displays output similar to the following:
//
// Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid
// Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid
// Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid
// </Snippet3>
34 changes: 34 additions & 0 deletions snippets/fsharp/System/Guid/Parse/parseexactex1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module parseexactex1

// <Snippet4>
open System

// Define an array of all format specifiers.
let formats =
[| "N"; "D"; "B"; "P"; "X" |]

let guid = Guid.NewGuid()

// Create an array of valid Guid string representations.
let stringGuids =
Array.map guid.ToString formats

// Parse the strings in the array using the "B" format specifier.
for stringGuid in stringGuids do
try
let newGuid = Guid.ParseExact(stringGuid, "B")
printfn $"Successfully parsed {stringGuid}"
with
| :? ArgumentNullException ->
printfn "The string to be parsed is null."
| :? FormatException ->
printfn $"Bad Format: {stringGuid}"

// The example displays output similar to the following:
//
// Bad Format: eb5c8c7d187a44e68afb81e854c39457
// Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457
// Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457}
// Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457)
// Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}}
// </Snippet4>
27 changes: 27 additions & 0 deletions snippets/fsharp/System/Guid/Parse/tryparseex1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module tryparseex1

// <Snippet2>
open System

let originalGuid = Guid.NewGuid()

// Create an array of string representations of the GUID.
let stringGuids =
[| originalGuid.ToString "B"
originalGuid.ToString "D"
originalGuid.ToString "X" |]

// Parse each string representation.
for stringGuid in stringGuids do
match Guid.TryParse stringGuid with
| true, newGuid ->
printfn $"Converted {stringGuid} to a Guid"
| _ ->
printfn $"Unable to convert {stringGuid} to a Guid"

// The example displays output similar to the following:
//
// Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid
// Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid
// Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid
// </Snippet2>
30 changes: 30 additions & 0 deletions snippets/fsharp/System/Guid/Parse/tryparseexactex1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module tryparseexactex1

// <Snippet5>
open System

// Define an array of all format specifiers.
let formats = [| "N"; "D"; "B"; "P"; "X" |]

let guid = Guid.NewGuid()

// Create an array of valid Guid string representations.
let stringGuids =
Array.map guid.ToString formats

// Parse the strings in the array using the "B" format specifier.
for stringGuid in stringGuids do
match Guid.TryParseExact(stringGuid, "B") with
| true, newGuid ->
printfn $"Successfully parsed {stringGuid}"
| _ ->
printfn $"Unable to parse '{stringGuid}'"

// The example displays output similar to the following:
//
// Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
// Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
// Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
// Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
// Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'
// </Snippet5>
Loading