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

Skip to content

System.Enum F# snippets #7696

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 15, 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
24 changes: 24 additions & 0 deletions snippets/fsharp/System/Enum/CompareTo/EnumCompareTo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//<snippet1>
type VehicleDoors =
| Motorbike = 0
| Sportscar = 2
| Sedan = 4
| Hatchback = 5

let myVeh = VehicleDoors.Sportscar
let yourVeh = VehicleDoors.Motorbike
let otherVeh = VehicleDoors.Sedan

printfn $"Does a {myVeh} have more doors than a {yourVeh}?"
printfn $"""{if myVeh.CompareTo yourVeh > 0 then "Yes" else "No"}\n"""

printfn $"Does a {myVeh} have more doors than a {otherVeh}?"
printfn $"""{if myVeh.CompareTo otherVeh > 0 then "Yes" else "No"}"""

// The example displays the following output:
// Does a Sportscar have more doors than a Motorbike?
// Yes
//
// Does a Sportscar have more doors than a Sedan?
// No
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Enum/CompareTo/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="EnumCompareTo.fs" />
</ItemGroup>
</Project>
48 changes: 48 additions & 0 deletions snippets/fsharp/System/Enum/Equals/EnumEquals.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module EnumEquals

//<snippet1>
open System

type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3

type Mammals =
| Cat = 0
| Dog = 1
| Horse = 2
| Dolphin = 3

let myPet = Mammals.Cat
let myColor = Colors.Red
let yourPet = Mammals.Dog
let yourColor = Colors.Red

printfn
$"""My favorite animal is a {myPet}
Your favorite animal is a {yourPet}
Do we like the same animal? {if myPet.Equals yourPet then "Yes" else "No"}

My favorite color is {myColor}
Your favorite color is {yourColor}
Do we like the same color? {if myColor.Equals yourColor then "Yes" else "No"}

The value of my color ({myColor}) is {Enum.Format(typeof<Colors>, myColor, "d")}
The value of my pet (a {myPet}) is {Enum.Format(typeof<Mammals>, myPet, "d")}
Even though they have the same value, are they equal? {if myColor.Equals myPet then "Yes" else "No"}"""

// The example displays the following output:
// My favorite animal is a Cat
// Your favorite animal is a Dog
// Do we like the same animal? No
//
// My favorite color is Red
// Your favorite color is Red
// Do we like the same color? Yes
//
// The value of my color (Red) is 0
// The value of my pet (a Cat) is 0
// Even though they have the same value, are they equal? No
//</snippet1>
25 changes: 25 additions & 0 deletions snippets/fsharp/System/Enum/Equals/enumequals1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module enumequals1

// <Snippet1>
type SledDog =
| Unknown = 0
| AlaskanMalamute = 1
| Malamute = 1
| Husky = 2
| SiberianHusky = 2

type WorkDog =
| Unknown = 0
| Newfoundland = 1
| GreatPyrennes = 2

let dog1 = SledDog.Malamute
let dog2 = SledDog.AlaskanMalamute
let dog3 = WorkDog.Newfoundland

printfn $"{dog1:F} ({dog1:D}) = {dog2:F} ({dog2:D}): {dog1.Equals dog2}"
printfn $"{dog1:F} ({dog1:D}) = {dog3:F} ({dog3:D}): {dog1.Equals dog3}"
// The example displays the following output:
// Malamute (1) = Malamute (1): True
// Malamute (1) = Newfoundland (1): False
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/Enum/Equals/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="EnumEquals.fs" />
<Compile Include="enumequals1.fs" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/Enum/Format/EnumFormat.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// <Snippet1>
open System

type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3

let myColor = Colors.Blue

printfn $"My favorite color is {myColor}."
printfn $"""The value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "d")}."""
printfn $"""The hex value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "x")}."""
// The example displays the following output:
// My favorite color is Blue.
// The value of my favorite color is 2.
// The hex value of my favorite color is 00000002.
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Enum/Format/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="EnumFormat.fs" />
</ItemGroup>
</Project>
21 changes: 21 additions & 0 deletions snippets/fsharp/System/Enum/GetName/EnumGetName.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//<Snippet1>
open System

type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3

type Styles =
| Plaid = 0
| Striped = 1
| Tartan = 2
| Corduroy = 3

printfn $"The 4th value of the Colors Enum is {Enum.GetName(typeof<Colors>, 3)}"
printfn $"The 4th value of the Styles Enum is {Enum.GetName(typeof<Styles>, 3)}"
// The example displays the following output:
// The 4th value of the Colors Enum is Yellow
// The 4th value of the Styles Enum is Corduroy
//</Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Enum/GetName/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="EnumGetName.fs" />
</ItemGroup>
</Project>
37 changes: 37 additions & 0 deletions snippets/fsharp/System/Enum/GetNames/EnumGetNames.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module EnumGetNames

//<snippet1>
open System

type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3

type Styles =
| Plaid = 0
| Striped = 1
| Tartan = 2
| Corduroy = 3

printfn "The members of the Colors enum are:"
for s in Enum.GetNames typeof<Colors> do
printfn $"{s}"

printfn "\nThe members of the Styles enum are:"
for s in Enum.GetNames typeof<Styles> do
printfn $"{s}"
// The example displays the following output:
// The members of the Colors enum are:
// Red
// Green
// Blue
// Yellow
//
// The members of the Styles enum are:
// Plaid
// Striped
// Tartan
// Corduroy
//</snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/Enum/GetNames/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="getnames1.fs" />
<Compile Include="EnumGetNames.fs" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/Enum/GetNames/getnames1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module getnames1

// <Snippet1>
open System

type SignMagnitude =
| Negative = -1
| Zero = 0
| Positive = 1

for name in Enum.GetNames typeof<SignMagnitude> do
let p = Enum.Parse(typeof<SignMagnitude>, name)
printfn $"{p,3:D} 0x{p:X} {name}"

// The example displays the following output:
// 0 0x00000000 Zero
// 1 0x00000001 Positive
// -1 0xFFFFFFFF Negative
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/Enum/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="getunderlyingtype1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <Snippet1>
open System

let displayEnumInfo (enumValue: Enum) =
let enumType = enumValue.GetType()
let underlyingType = Enum.GetUnderlyingType enumType
printfn $"{enumValue,-10} {enumType.Name, 18} {underlyingType.Name,15}"

let enumValues: Enum list =
[ ConsoleColor.Red; DayOfWeek.Monday
MidpointRounding.ToEven; PlatformID.Win32NT
DateTimeKind.Utc; StringComparison.Ordinal ]

printfn "%-10s %18s %15s\n" "Member" "Enumeration" "Underlying Type"
for enumValue in enumValues do
displayEnumInfo enumValue

// The example displays the following output:
// Member Enumeration Underlying Type
//
// Red ConsoleColor Int32
// Monday DayOfWeek Int32
// ToEven MidpointRounding Int32
// Win32NT PlatformID Int32
// Utc DateTimeKind Int32
// Ordinal StringComparison Int32
// </Snippet1>
38 changes: 38 additions & 0 deletions snippets/fsharp/System/Enum/GetValues/EnumGetValues.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module EnumGetValues

//<snippet1>
open System

type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3

type Styles =
| Plaid = 0
| Striped = 23
| Tartan = 65
| Corduroy = 78

printfn $"The values of the Colors Enum are:"
for i in Enum.GetValues typeof<Colors> do
printfn $"{i}"

printfn "\nThe values of the Styles Enum are:"
for i in Enum.GetValues typeof<Styles> do
printfn $"{i}"

// The example produces the following output:
// The values of the Colors Enum are:
// 0
// 1
// 2
// 3
//
// The values of the Styles Enum are:
// 0
// 23
// 65
// 78
//</snippet1>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/Enum/GetValues/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="getvalues1.fs" />
<Compile Include="getvalues_reflectiononly.fs" />
<Compile Include="EnumGetValues.fs" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions snippets/fsharp/System/Enum/GetValues/getvalues1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module getvalues1

// <Snippet1>
open System

type SignMagnitude =
| Negative = -1
| Zero = 0
| Positive = 1

for value in Enum.GetValues typeof<SignMagnitude> do
printfn $"{value :?> int,3} 0x{value :?> int:X8} {value :?> SignMagnitude}"
// The example displays the following output:
// 0 0x00000000 Zero
// 1 0x00000001 Positive
// -1 0xFFFFFFFF Negative
// </Snippet1>
Loading