diff --git a/snippets/fsharp/System/Enum/CompareTo/EnumCompareTo.fs b/snippets/fsharp/System/Enum/CompareTo/EnumCompareTo.fs new file mode 100644 index 00000000000..c144332221d --- /dev/null +++ b/snippets/fsharp/System/Enum/CompareTo/EnumCompareTo.fs @@ -0,0 +1,24 @@ +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/CompareTo/fs.fsproj b/snippets/fsharp/System/Enum/CompareTo/fs.fsproj new file mode 100644 index 00000000000..b2b252f10b9 --- /dev/null +++ b/snippets/fsharp/System/Enum/CompareTo/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Equals/EnumEquals.fs b/snippets/fsharp/System/Enum/Equals/EnumEquals.fs new file mode 100644 index 00000000000..9128f8d84dd --- /dev/null +++ b/snippets/fsharp/System/Enum/Equals/EnumEquals.fs @@ -0,0 +1,48 @@ +module EnumEquals + +// +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, myColor, "d")} +The value of my pet (a {myPet}) is {Enum.Format(typeof, 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Equals/enumequals1.fs b/snippets/fsharp/System/Enum/Equals/enumequals1.fs new file mode 100644 index 00000000000..ca67a073d80 --- /dev/null +++ b/snippets/fsharp/System/Enum/Equals/enumequals1.fs @@ -0,0 +1,25 @@ +module enumequals1 + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Equals/fs.fsproj b/snippets/fsharp/System/Enum/Equals/fs.fsproj new file mode 100644 index 00000000000..91161989b2c --- /dev/null +++ b/snippets/fsharp/System/Enum/Equals/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Format/EnumFormat.fs b/snippets/fsharp/System/Enum/Format/EnumFormat.fs new file mode 100644 index 00000000000..60fae225850 --- /dev/null +++ b/snippets/fsharp/System/Enum/Format/EnumFormat.fs @@ -0,0 +1,19 @@ +// +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, myColor, "d")}.""" +printfn $"""The hex value of my favorite color is {Enum.Format(typeof, 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. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Format/fs.fsproj b/snippets/fsharp/System/Enum/Format/fs.fsproj new file mode 100644 index 00000000000..7e7e1e23d28 --- /dev/null +++ b/snippets/fsharp/System/Enum/Format/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetName/EnumGetName.fs b/snippets/fsharp/System/Enum/GetName/EnumGetName.fs new file mode 100644 index 00000000000..c39e9ab59e4 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetName/EnumGetName.fs @@ -0,0 +1,21 @@ +// +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, 3)}" +printfn $"The 4th value of the Styles Enum is {Enum.GetName(typeof, 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetName/fs.fsproj b/snippets/fsharp/System/Enum/GetName/fs.fsproj new file mode 100644 index 00000000000..07c075ce221 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetName/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetNames/EnumGetNames.fs b/snippets/fsharp/System/Enum/GetNames/EnumGetNames.fs new file mode 100644 index 00000000000..938b4b22aad --- /dev/null +++ b/snippets/fsharp/System/Enum/GetNames/EnumGetNames.fs @@ -0,0 +1,37 @@ +module EnumGetNames + +// +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 do + printfn $"{s}" + +printfn "\nThe members of the Styles enum are:" +for s in Enum.GetNames typeof 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetNames/fs.fsproj b/snippets/fsharp/System/Enum/GetNames/fs.fsproj new file mode 100644 index 00000000000..f470f1a0405 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetNames/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetNames/getnames1.fs b/snippets/fsharp/System/Enum/GetNames/getnames1.fs new file mode 100644 index 00000000000..27653c73815 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetNames/getnames1.fs @@ -0,0 +1,19 @@ +module getnames1 + +// +open System + +type SignMagnitude = + | Negative = -1 + | Zero = 0 + | Positive = 1 + +for name in Enum.GetNames typeof do + let p = Enum.Parse(typeof, name) + printfn $"{p,3:D} 0x{p:X} {name}" + +// The example displays the following output: +// 0 0x00000000 Zero +// 1 0x00000001 Positive +// -1 0xFFFFFFFF Negative +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetUnderlyingType/fs.fsproj b/snippets/fsharp/System/Enum/GetUnderlyingType/fs.fsproj new file mode 100644 index 00000000000..252443b4bc1 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetUnderlyingType/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetUnderlyingType/getunderlyingtype1.fs b/snippets/fsharp/System/Enum/GetUnderlyingType/getunderlyingtype1.fs new file mode 100644 index 00000000000..19974c6ff99 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetUnderlyingType/getunderlyingtype1.fs @@ -0,0 +1,27 @@ +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetValues/EnumGetValues.fs b/snippets/fsharp/System/Enum/GetValues/EnumGetValues.fs new file mode 100644 index 00000000000..08c1515e865 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetValues/EnumGetValues.fs @@ -0,0 +1,38 @@ +module EnumGetValues + +// +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 do + printfn $"{i}" + +printfn "\nThe values of the Styles Enum are:" +for i in Enum.GetValues typeof 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetValues/fs.fsproj b/snippets/fsharp/System/Enum/GetValues/fs.fsproj new file mode 100644 index 00000000000..46039814521 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetValues/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetValues/getvalues1.fs b/snippets/fsharp/System/Enum/GetValues/getvalues1.fs new file mode 100644 index 00000000000..d03fb9b7dea --- /dev/null +++ b/snippets/fsharp/System/Enum/GetValues/getvalues1.fs @@ -0,0 +1,17 @@ +module getvalues1 + +// +open System + +type SignMagnitude = + | Negative = -1 + | Zero = 0 + | Positive = 1 + +for value in Enum.GetValues typeof 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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/GetValues/getvalues_reflectiononly.fs b/snippets/fsharp/System/Enum/GetValues/getvalues_reflectiononly.fs new file mode 100644 index 00000000000..3b43c9b9f61 --- /dev/null +++ b/snippets/fsharp/System/Enum/GetValues/getvalues_reflectiononly.fs @@ -0,0 +1,37 @@ +module getvalues_reflectiononly + +// +open System +open System.Reflection + +let assem = Assembly.ReflectionOnlyLoadFrom @".\Enumerations.dll" +let typ = assem.GetType "Pets" +let fields = typ.GetFields() + +for field in fields do + if not (field.Name.Equals "value__") then + + printfn $"""{field.Name + ":",-9} {field.GetRawConstantValue()}""" +// The example displays the following output: +// None: 0 +// Dog: 1 +// Cat: 2 +// Rodent: 4 +// Bird: 8 +// Fish: 16 +// Reptile: 32 +// Other: 64 +// + +// +[] +type Pets = + | None = 0 + | Dog = 1 + | Cat = 2 + | Rodent = 4 + | Bird = 8 + | Fish = 16 + | Reptile = 32 + | Other = 64 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/HasFlag/fs.fsproj b/snippets/fsharp/System/Enum/HasFlag/fs.fsproj new file mode 100644 index 00000000000..601d0aac57c --- /dev/null +++ b/snippets/fsharp/System/Enum/HasFlag/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/HasFlag/hasflag0.fs b/snippets/fsharp/System/Enum/HasFlag/hasflag0.fs new file mode 100644 index 00000000000..21f142012cf --- /dev/null +++ b/snippets/fsharp/System/Enum/HasFlag/hasflag0.fs @@ -0,0 +1,32 @@ +module hasflag0 + +// +open System + +[] +type Pets = + | None = 0 + | Dog = 1 + | Cat = 2 + | Bird = 4 + | Rabbit = 8 + | Other = 16 + +let petsInFamilies = [| Pets.None; Pets.Dog ||| Pets.Cat; Pets.Dog |] +let mutable familiesWithoutPets = 0 +let mutable familiesWithDog = 0 + +for petsInFamily in petsInFamilies do + // Count families that have no pets. + if petsInFamily.Equals Pets.None then + familiesWithoutPets <- familiesWithoutPets + 1 + // Of families with pets, count families that have a dog. + elif petsInFamily.HasFlag Pets.Dog then + familiesWithDog <- familiesWithDog + 1 + +printfn $"{familiesWithoutPets} of {petsInFamilies.Length} families in the sample have no pets." +printfn $"{familiesWithDog} of {petsInFamilies} families in the sample have a dog." +// The example displays the following output: +// 1 of 3 families in the sample have no pets. +// 2 of 3 families in the sample have a dog. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/HasFlag/hasflag1.fs b/snippets/fsharp/System/Enum/HasFlag/hasflag1.fs new file mode 100644 index 00000000000..190c3e2d90c --- /dev/null +++ b/snippets/fsharp/System/Enum/HasFlag/hasflag1.fs @@ -0,0 +1,23 @@ +module hasflag1 + +// +open System + +[] +type DinnerItems = + | None = 0 + | Entree = 1 + | Appetizer = 2 + | Side = 4 + | Dessert = 8 + | Beverage = 16 + | BarBeverage = 32 + +let myOrder = + DinnerItems.Appetizer ||| DinnerItems.Entree ||| DinnerItems.Beverage ||| DinnerItems.Dessert +let flagValue = + DinnerItems.Entree ||| DinnerItems.Beverage +printfn $"{myOrder} includes {flagValue}: {myOrder.HasFlag flagValue}" +// The example displays the following output: +// Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/IsDefined/fs.fsproj b/snippets/fsharp/System/Enum/IsDefined/fs.fsproj new file mode 100644 index 00000000000..95841cf3cd1 --- /dev/null +++ b/snippets/fsharp/System/Enum/IsDefined/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/IsDefined/isdefined1.fs b/snippets/fsharp/System/Enum/IsDefined/isdefined1.fs new file mode 100644 index 00000000000..9e2bd99b3d6 --- /dev/null +++ b/snippets/fsharp/System/Enum/IsDefined/isdefined1.fs @@ -0,0 +1,53 @@ +module isdefnied1 + +// +open System + +[] +type PetType = + | None = 0 + | Dog = 1 + | Cat = 2 + | Rodent = 4 + | Bird = 8 + | Reptile = 16 + | Other = 32 + +[] +let main _ = + // Call IsDefined with underlying integral value of member. + let value = 1 + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + // Call IsDefined with invalid underlying integral value. + let value = 64 + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + // Call IsDefined with string containing member name. + let value = "Rodent" + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + // Call IsDefined with a variable of type PetType. + let value = PetType.Dog + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + let value = PetType.Dog ||| PetType.Cat + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + // Call IsDefined with uppercase member name. + let value = "None" + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + let value = "NONE" + printfn $"{value}: {Enum.IsDefined(typeof, value)}" + // Call IsDefined with combined value + let value = PetType.Dog ||| PetType.Bird + printfn $"{value:D}: {Enum.IsDefined(typeof, value)}" + let value = value.ToString() + printfn $"{value:D}: {Enum.IsDefined(typeof, value)}" + 0 +// The example displays the following output: +// 1: True +// 64: False +// Rodent: True +// Dog: True +// Dog, Cat: False +// None: True +// NONE: False +// 9: False +// Dog, Bird: False +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/IsDefined/isdefined2.fs b/snippets/fsharp/System/Enum/IsDefined/isdefined2.fs new file mode 100644 index 00000000000..e34d3b1f347 --- /dev/null +++ b/snippets/fsharp/System/Enum/IsDefined/isdefined2.fs @@ -0,0 +1,22 @@ +module isdefined2 + +// +open System + +[] +type Pets = + | None = 0 + | Dog = 1 + | Cat = 2 + | Bird = 4 + | Rodent = 8 + | Other = 16 + +let value = Pets.Dog ||| Pets.Cat +printfn $"{value:D} Exists: {Pets.IsDefined(typeof, value)}" +let name = string value +printfn $"{name} Exists: {Pets.IsDefined(typeof, name)}" +// The example displays the following output: +// 3 Exists: False +// Dog, Cat Exists: False +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/EnumMain.fs b/snippets/fsharp/System/Enum/Overview/EnumMain.fs new file mode 100644 index 00000000000..5f0424cc3fb --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/EnumMain.fs @@ -0,0 +1,42 @@ +module EnumMain + +// +open System + +type Days = + | Saturday = 0 + | Sunday = 1 + | Monday = 2 + | Tuesday = 3 + | Wednesday = 4 + | Thursday = 5 + | Friday = 6 + +type BoilingPoints = + | Celsius = 100 + | Fahrenheit = 212 + +[] +type Colors = + | Red = 1 + | Green = 2 + | Blue = 4 + | Yellow = 8 + +let weekdays = typeof +let boiling = typeof + +printfn "The days of the week, and their corresponding values in the Days Enum are:" + +for s in Enum.GetNames weekdays do + printfn $"""{s,-11}= {Enum.Format(weekdays, Enum.Parse(weekdays, s), "d")}""" + +printfn "\nEnums can also be created which have values that represent some meaningful amount." +printfn "The BoilingPoints Enum defines the following items, and corresponding values:" + +for s in Enum.GetNames boiling do + printfn $"""{s,-11}= {Enum.Format(boiling, Enum.Parse(boiling, s), "d")}""" + +let myColors = Colors.Red ||| Colors.Blue ||| Colors.Yellow +printfn $"\nmyColors holds a combination of colors. Namely: {myColors}" +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/Extensions.fs b/snippets/fsharp/System/Enum/Overview/Extensions.fs new file mode 100644 index 00000000000..8d9c1ba6dc1 --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/Extensions.fs @@ -0,0 +1,39 @@ +module Extensions + +// +open System +open System.Runtime.CompilerServices +// Define an enumeration to represent student grades. +type Grades = + | F = 0 + | D = 1 + | C = 2 + | B = 3 + | A = 4 + +let mutable minPassing = Grades.D + +// Define an extension method for the Grades enumeration. +[] +type Extensions = + [] + static member Passing(grade) = grade >= minPassing + +let g1 = Grades.D +let g2 = Grades.F +printfn $"""{g1} {if g1.Passing() then "is" else "is not"} a passing grade.""" +printfn $"""{g2} {if g2.Passing() then "is" else "is not"} a passing grade.""" + +minPassing <- Grades.C +printfn "\nRaising the bar!\n" +printfn $"""{g1} {if g1.Passing() then "is" else "is not"} a passing grade.""" +printfn $"""{g2} {if g2.Passing() then "is" else "is not"} a passing grade.""" +// The exmaple displays the following output: +// D is a passing grade. +// F is not a passing grade. +// +// Raising the bar! +// +// D is not a passing grade. +// F is not a passing grade. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/class1.fs b/snippets/fsharp/System/Enum/Overview/class1.fs new file mode 100644 index 00000000000..2287c437cad --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/class1.fs @@ -0,0 +1,15 @@ +module class1 + +// +type ArrivalStatus = + | Late = -1 + | OnTime = 0 + | Early = 1 +// + +// +let status = ArrivalStatus.OnTime +printfn $"Arrival Status: {status} ({status:D})" +// The example displays the following output: +// Arrival Status: OnTime (0) +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/class2.fs b/snippets/fsharp/System/Enum/Overview/class2.fs new file mode 100644 index 00000000000..d1e5cb86d7d --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/class2.fs @@ -0,0 +1,32 @@ +module class2 + +type ArrivalStatus = + | Late = -1 + | OnTime = 0 + | Early = 1 + +// +let status1 = ArrivalStatus() +printfn $"Arrival Status: {status1} ({status1:D})" +// The example displays the following output: +// Arrival Status: OnTime (0) +// + +// +let status2 = enum 1 +printfn $"Arrival Status: {status2} ({status2:D})" +// The example displays the following output: +// Arrival Status: Early (1) +// + +// +let value3 = 2 +let status3 = enum value3 + +let value4 = int status3 +// + +// +let number = -1 +let arrived = ArrivalStatus.ToObject(typeof, number) :?> ArrivalStatus +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/classbitwise1.fs b/snippets/fsharp/System/Enum/Overview/classbitwise1.fs new file mode 100644 index 00000000000..19c04d8e060 --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/classbitwise1.fs @@ -0,0 +1,55 @@ +module classbitwise1 + +open System + +// +[] +type Pets = + | None = 0 + | Dog = 1 + | Cat = 2 + | Bird = 4 + | Rodent = 8 + | Reptile = 16 + | Other = 32 +// + +// +let familyPets = Pets.Dog ||| Pets.Cat +printfn $"Pets: {familyPets:G} ({familyPets:D})" +// The example displays the following output: +// Pets: Dog, Cat (3) +// + +let showHasFlag () = + // + let familyPets = Pets.Dog ||| Pets.Cat + if familyPets.HasFlag Pets.Dog then + printfn "The family has a dog." + // The example displays the following output: + // The family has a dog. + // + +let showIfSet () = + // + let familyPets = Pets.Dog ||| Pets.Cat + if (familyPets &&& Pets.Dog) = Pets.Dog then + printfn "The family has a dog." + // The example displays the following output: + // The family has a dog. + // + +let testForNone () = + // + let familyPets = Pets.Dog ||| Pets.Cat + if familyPets = Pets.None then + printfn "The family has no pets." + else + printfn "The family has pets." + // The example displays the following output: + // The family has pets. + // + +showHasFlag () +showIfSet () +testForNone () diff --git a/snippets/fsharp/System/Enum/Overview/classconversion1.fs b/snippets/fsharp/System/Enum/Overview/classconversion1.fs new file mode 100644 index 00000000000..f1725e56621 --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/classconversion1.fs @@ -0,0 +1,27 @@ +module classconversion1 + +// +open System + +type ArrivalStatus = + | Unknown = -3 + | Late = -1 + | OnTime = 0 + | Early = 1 + +let values = [ -3; -1; 0; 1; 5; Int32.MaxValue ] +for value in values do + let status = + if Enum.IsDefined(typeof, value) then + enum value + else + ArrivalStatus.Unknown + printfn $"Converted {value:N0} to {status}" +// The example displays the following output: +// Converted -3 to Unknown +// Converted -1 to Late +// Converted 0 to OnTime +// Converted 1 to Early +// Converted 5 to Unknown +// Converted 2,147,483,647 to Unknown +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/classconversion2.fs b/snippets/fsharp/System/Enum/Overview/classconversion2.fs new file mode 100644 index 00000000000..6ec9b0b11f7 --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/classconversion2.fs @@ -0,0 +1,17 @@ +module classconversion2 + +open System + +type ArrivalStatus = + | Unknown = -3 + | Late = -1 + | OnTime = 0 + | Early = 1 + +// +let status = ArrivalStatus.Early +let number = Convert.ChangeType(status, Enum.GetUnderlyingType typeof) +printfn $"Converted {status} to {number}" +// The example displays the following output: +// Converted Early to 1 +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/classformat1.fs b/snippets/fsharp/System/Enum/Overview/classformat1.fs new file mode 100644 index 00000000000..2f9ddb3923b --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/classformat1.fs @@ -0,0 +1,22 @@ +module classformat1 + +open System + +type ArrivalStatus = + | Unknown = -3 + | Late = -1 + | OnTime = 0 + | Early = 1 + +// +let formats = [ "G"; "F"; "D"; "X" ] +let status = ArrivalStatus.Late +for fmt in formats do + printfn $"{status.ToString fmt}" + +// The example displays the following output: +// Late +// Late +// -1 +// FFFFFFFF +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/classiterate.fs b/snippets/fsharp/System/Enum/Overview/classiterate.fs new file mode 100644 index 00000000000..291674b018b --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/classiterate.fs @@ -0,0 +1,43 @@ +module classinterate + +open System + +type ArrivalStatus = + | Unknown = -3 + | Late = -1 + | OnTime = 0 + | Early = 1 + +let getEnumByName () = + // + let names = Enum.GetNames typeof + printfn $"Members of {nameof ArrivalStatus}:" + let names = Array.sort names + for name in names do + let status = Enum.Parse(typeof, name) :?> ArrivalStatus + printfn $" {status} ({status:D})" + // The example displays the following output: + // Members of ArrivalStatus: + // Early (1) + // Late (-1) + // OnTime (0) + // Unknown (-3) + // + +let getEnumByValue () = + // + let values = Enum.GetValues typeof + printfn $"Members of {nameof ArrivalStatus}:" + for status in values do + printfn $" {status} ({status:D})" + // The example displays the following output: + // Members of ArrivalStatus: + // OnTime (0) + // Early (1) + // Unknown (-3) + // Late (-1) + // + +getEnumByName () +printfn "-----" +getEnumByValue () \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/classparse1.fs b/snippets/fsharp/System/Enum/Overview/classparse1.fs new file mode 100644 index 00000000000..a976f5e5c4e --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/classparse1.fs @@ -0,0 +1,40 @@ +module classparse1 + +open System + +type ArrivalStatus = + | Unknown = -3 + | Late = -1 + | OnTime = 0 + | Early = 1 + +// +let number = "-1" +let name = "Early" + +try + let status1 = Enum.Parse(typeof, number) :?> ArrivalStatus + let status1 = + if not (Enum.IsDefined(typeof, status1) ) then + ArrivalStatus.Unknown + else + status1 + + printfn $"Converted '{number}' to {status1}" +with :? FormatException -> + printfn $"Unable to convert '{number}' to an ArrivalStatus value." + +match Enum.TryParse name with +| true, status2 -> + let status2 = + if not (Enum.IsDefined(typeof, status2) ) then + ArrivalStatus.Unknown + else + status2 + printfn $"Converted '{name}' to {status2}" +| _ -> + printfn $"Unable to convert '{number}' to an ArrivalStatus value." +// The example displays the following output: +// Converted '-1' to Late +// Converted 'Early' to Early +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Overview/fs.fsproj b/snippets/fsharp/System/Enum/Overview/fs.fsproj new file mode 100644 index 00000000000..cf4ae594400 --- /dev/null +++ b/snippets/fsharp/System/Enum/Overview/fs.fsproj @@ -0,0 +1,19 @@ + + + Exe + net6.0 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Parse/EnumParse.fs b/snippets/fsharp/System/Enum/Parse/EnumParse.fs new file mode 100644 index 00000000000..869cdc16e54 --- /dev/null +++ b/snippets/fsharp/System/Enum/Parse/EnumParse.fs @@ -0,0 +1,30 @@ +module EnumParse + +// +open System + +[] +type Colors = + | Red = 1 + | Green = 2 + | Blue = 4 + | Yellow = 8 + +printfn "The entries of the Colors enumeration are:" +for colorName in Enum.GetNames typeof do + printfn $"{colorName} = {Enum.Parse(typeof, colorName):D}" +printfn "" + +let orange = Enum.Parse(typeof, "Red, Yellow") :?> Colors +printfn $"The orange value {orange:D} has the combined entries of {orange}" + +// This code example produces the following results: +// The entries of the Colors Enum are: +// Red = 1 +// Green = 2 +// Blue = 4 +// Yellow = 8 +// +// The orange value 9 has the combined entries of Red, Yellow + +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Parse/ParseExample1.fs b/snippets/fsharp/System/Enum/Parse/ParseExample1.fs new file mode 100644 index 00000000000..97d6d469f7a --- /dev/null +++ b/snippets/fsharp/System/Enum/Parse/ParseExample1.fs @@ -0,0 +1,31 @@ +module ParseExample1 + +// +open System + +[] +type Colors = + | None = 0 + | Red = 1 + | Green = 2 + | Blue = 4 + +let colorStrings = [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ] +for colorString in colorStrings do + try + let colorValue = Enum.Parse(typeof, colorString) :?> Colors + if Enum.IsDefined(typeof, colorValue) || (string colorValue).Contains "," then + printfn $"Converted '{colorString}' to {colorValue}." + else + printfn $"{colorString} is not an underlying value of the Colors enumeration." + with :? ArgumentException -> + printfn $"'{colorString}' is not a member of the Colors enumeration." +// The example displays the following output: +// Converted '0' to None. +// Converted '2' to Green. +// 8 is not an underlying value of the Colors enumeration. +// 'blue' is not a member of the Colors enumeration. +// Converted 'Blue' to Blue. +// 'Yellow' is not a member of the Colors enumeration. +// Converted 'Red, Green' to Red, Green. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Parse/ParseExample2.fs b/snippets/fsharp/System/Enum/Parse/ParseExample2.fs new file mode 100644 index 00000000000..912654dd16e --- /dev/null +++ b/snippets/fsharp/System/Enum/Parse/ParseExample2.fs @@ -0,0 +1,32 @@ +module PraseExample2 + +// +open System + +[] +type Colors = + | None = 0 + | Red = 1 + | Green = 2 + | Blue = 4 + +let colorStrings = [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ] +for colorString in colorStrings do + try + let colorValue = Enum.Parse(typeof, colorString, true) :?> Colors + if Enum.IsDefined(typeof, colorValue) || (string colorValue).Contains "," then + printfn $"Converted '{colorString}' to {colorValue}." + else + printfn $"{colorString} is not an underlying value of the Colors enumeration." + with :? ArgumentException -> + printfn $"{colorString} is not a member of the Colors enumeration." + +// The example displays the following output: +// Converted '0' to None. +// Converted '2' to Green. +// 8 is not an underlying value of the Colors enumeration. +// Converted 'blue' to Blue. +// Converted 'Blue' to Blue. +// Yellow is not a member of the Colors enumeration. +// Converted 'Red, Green' to Red, Green. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/Parse/fs.fsproj b/snippets/fsharp/System/Enum/Parse/fs.fsproj new file mode 100644 index 00000000000..8a1050e0787 --- /dev/null +++ b/snippets/fsharp/System/Enum/Parse/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/ToString/fs.fsproj b/snippets/fsharp/System/Enum/ToString/fs.fsproj new file mode 100644 index 00000000000..9a37d98c482 --- /dev/null +++ b/snippets/fsharp/System/Enum/ToString/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/ToString/source.fs b/snippets/fsharp/System/Enum/ToString/source.fs new file mode 100644 index 00000000000..4e7ffc71dc7 --- /dev/null +++ b/snippets/fsharp/System/Enum/ToString/source.fs @@ -0,0 +1,13 @@ +module source + +// +type Colors = + | Red = 1 + | Blue = 2 + +let myColors = Colors.Red +printfn $"The value of this instance is '{myColors.ToString()}'" + +// Output. +// The value of this instance is 'Red'. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/ToString/tostr.fs b/snippets/fsharp/System/Enum/ToString/tostr.fs new file mode 100644 index 00000000000..a7a83af4bcf --- /dev/null +++ b/snippets/fsharp/System/Enum/ToString/tostr.fs @@ -0,0 +1,50 @@ +module tostr + +// +// Sample for Enum.ToString(String) +open System + +type Colors = + | Red = 0 + | Green = 1 + | Blue = 2 + | Yellow = 12 + +let myColor = Colors.Yellow + +printfn $"""Colors.Red = {Colors.Red.ToString "d"}""" +printfn $"""Colors.Green = {Colors.Green.ToString "d"}""" +printfn $"""Colors.Blue = {Colors.Blue.ToString "d"}""" +printfn $"""Colors.Yellow = {Colors.Yellow.ToString "d"}""" + +printfn "\nmyColor = Colors.Yellow\n" + +printfn $"""myColor.ToString("g") = {myColor.ToString "g"}""" +printfn $"""myColor.ToString("G") = {myColor.ToString "G"}""" + +printfn $"""myColor.ToString("x") = {myColor.ToString "x"}""" +printfn $"""myColor.ToString("X") = {myColor.ToString "X"}""" + +printfn $"""myColor.ToString("d") = {myColor.ToString "d"}""" +printfn $"""myColor.ToString("D") = {myColor.ToString "d"}""" + +printfn $"""myColor.ToString("f") = {myColor.ToString "f"}""" +printfn $"""myColor.ToString("F") = {myColor.ToString "F"}""" + +// This example produces the following results: +// Colors.Red = 0 +// Colors.Green = 1 +// Colors.Blue = 2 +// Colors.Yellow = 12 +// +// myColor = Colors.Yellow +// +// myColor.ToString("g") = Yellow +// myColor.ToString("G") = Yellow +// myColor.ToString("x") = 0000000C +// myColor.ToString("X") = 0000000C +// myColor.ToString "d" = 12 +// myColor.ToString "d" = 12 +// myColor.ToString("f") = Yellow +// myColor.ToString("F") = Yellow +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs b/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs new file mode 100644 index 00000000000..02ef62647e0 --- /dev/null +++ b/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs @@ -0,0 +1,24 @@ +module tostringby + +// +type Shade = + | White = 0 + | Gray = 1 + | Grey = 1 + | Black = 2 +// + +let callDefault () = + // + let shadeName = (enum 1).ToString() + // + printfn $"{shadeName}" + +let callWithFormatString () = + // + let shadeName = (enum 1).ToString "F" + // + printfn $"{shadeName}" + +callDefault () +callWithFormatString () diff --git a/snippets/fsharp/System/Enum/TryParseTEnum/fs.fsproj b/snippets/fsharp/System/Enum/TryParseTEnum/fs.fsproj new file mode 100644 index 00000000000..082dd47f2be --- /dev/null +++ b/snippets/fsharp/System/Enum/TryParseTEnum/fs.fsproj @@ -0,0 +1,11 @@ + + + Exe + net6.0 + + + + + + + \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/TryParseTEnum/tryparse1.fs b/snippets/fsharp/System/Enum/TryParseTEnum/tryparse1.fs new file mode 100644 index 00000000000..993ae33f2d2 --- /dev/null +++ b/snippets/fsharp/System/Enum/TryParseTEnum/tryparse1.fs @@ -0,0 +1,32 @@ +module tryparse1 + +// +open System + +[] +type Colors = + | None = 0 + | Red = 1 + | Green = 2 + | Blue = 4 + +let colorStrings = + [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ] +for colorString in colorStrings do + match Enum.TryParse colorString with + | true, colorValue -> + if Enum.IsDefined(typeof, colorValue) || (string colorValue).Contains "," then + printfn $"Converted '{colorString}' to {colorValue}." + else + printfn $"{colorString} is not an underlying value of the Colors enumeration." + | _ -> + printfn $"{colorString} is not a member of the Colors enumeration." +// The example displays the following output: +// Converted '0' to None. +// Converted '2' to Green. +// 8 is not an underlying value of the Colors enumeration. +// blue is not a member of the Colors enumeration. +// Converted 'Blue' to Blue. +// Yellow is not a member of the Colors enumeration. +// Converted 'Red, Green' to Red, Green. +// \ No newline at end of file diff --git a/snippets/fsharp/System/Enum/TryParseTEnum/tryparse2.fs b/snippets/fsharp/System/Enum/TryParseTEnum/tryparse2.fs new file mode 100644 index 00000000000..5ad898cf545 --- /dev/null +++ b/snippets/fsharp/System/Enum/TryParseTEnum/tryparse2.fs @@ -0,0 +1,32 @@ +module tryparse2 + +// +open System + +[] +type Colors = + | None = 0 + | Red = 1 + | Green = 2 + | Blue = 4 + +let colorStrings = + [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ] +for colorString in colorStrings do + match Enum.TryParse(colorString, true) with + | true, colorValue -> + if Enum.IsDefined(typeof, colorValue) || (string colorValue).Contains "," then + printfn $"Converted '{colorString}' to {colorValue}." + else + printfn $"{colorString} is not an underlying value of the Colors enumeration." + | _ -> + printfn $"{colorString} is not a member of the Colors enumeration." +// The example displays the following output: +// Converted '0' to None. +// Converted '2' to Green. +// 8 is not an underlying value of the Colors enumeration. +// Converted 'blue' to Blue. +// Converted 'Blue' to Blue. +// Yellow is not a member of the Colors enumeration. +// Converted 'Red, Green' to Red, Green. +// \ No newline at end of file diff --git a/xml/System/Enum.xml b/xml/System/Enum.xml index 6d245680e2a..078dabdabcf 100644 --- a/xml/System/Enum.xml +++ b/xml/System/Enum.xml @@ -71,7 +71,7 @@ is used. is the base class for all enumerations in the .NET Framework. Enumeration types are defined by the `enum` keyword in C# and the `Enum`...`End Enum` construct in Visual Basic. + An enumeration is a set of named constants whose underlying type is any integral type. If no underlying type is explicitly declared, is used. is the base class for all enumerations in the .NET Framework. Enumeration types are defined by the `enum` keyword in C#, the `Enum`...`End Enum` construct in Visual Basic, and the `type` keyword in F#. provides methods for comparing instances of this class, converting the value of an instance to its string representation, converting the string representation of a number to an instance of this class, and creating an instance of a specified enumeration and value. @@ -92,9 +92,10 @@ ## Creating an enumeration type - Programming languages typically provide syntax to declare an enumeration that consists of a set of named constants and their values. The following example illustrates the syntax used by C# and Visual Basic to define an enumeration. It creates an enumeration named `ArrivalStatus` that has three members: `ArrivalStatus.Early`, `ArrivalStatus.OnTime`, and `ArrivalStatus.Late`. Note that in both cases, the enumeration does not explicitly inherit from ; the inheritance relationship is handled implicitly by the compiler. + Programming languages typically provide syntax to declare an enumeration that consists of a set of named constants and their values. The following example illustrates the syntax used by C#, F#, and Visual Basic to define an enumeration. It creates an enumeration named `ArrivalStatus` that has three members: `ArrivalStatus.Early`, `ArrivalStatus.OnTime`, and `ArrivalStatus.Late`. Note that in all cases, the enumeration does not explicitly inherit from ; the inheritance relationship is handled implicitly by the compiler. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/class1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/class1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/class1.vb" id="Snippet1"::: > [!WARNING] @@ -105,6 +106,7 @@ You can instantiate an enumeration type just as you instantiate any other value type: by declaring a variable and assigning one of the enumeration's constants to it. The following example instantiates an `ArrivalStatus` whose value is `ArrivalStatus.OnTime`. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/class1.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/class1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/class1.vb" id="Snippet2"::: You can also instantiate an enumeration value in the following ways: @@ -112,11 +114,13 @@ - By using a particular programming language's features to cast (as in C#) or convert (as in Visual Basic) an integer value to an enumeration value. The following example creates an `ArrivalStatus` object whose value is `ArrivalStatus.Early` in this way. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/class2.cs" id="Snippet4"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/class2.fs" id="Snippet4"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/class2.vb" id="Snippet4"::: - By calling its implicit parameterless constructor. As the following example shows, in this case the underlying value of the enumeration instance is 0. However, this is not necessarily the value of a valid constant in the enumeration. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/class2.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/class2.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/class2.vb" id="Snippet3"::: - By calling the or method to parse a string that contains the name of a constant in the enumeration. For more information, see the [Parsing Enumeration Values](#parsing) section. @@ -143,31 +147,36 @@ ### Performing conversions - You can convert between an enumeration member and its underlying type by using a casting (in C#) or conversion (in Visual Basic) operator. The following example uses casting or conversion operators to perform conversions both from an integer to an enumeration value and from an enumeration value to an integer. + You can convert between an enumeration member and its underlying type by using a casting (in C# and F#), or conversion (in Visual Basic) operator. In F#, the `enum` function is also used. The following example uses casting or conversion operators to perform conversions both from an integer to an enumeration value and from an enumeration value to an integer. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/class2.cs" id="Snippet5"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/class2.fs" id="Snippet5"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/class2.vb" id="Snippet5"::: The class also includes a method that converts a value of any integral type to an enumeration value. The following example uses the method to convert an to an `ArrivalStatus` value. Note that, because the returns a value of type , the use of a casting or conversion operator may still be necessary to cast the object to the enumeration type. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/class2.cs" id="Snippet6"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/class2.fs" id="Snippet6"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/class2.vb" id="Snippet6"::: When converting an integer to an enumeration value, it is possible to assign a value that is not actually a member of the enumeration. To prevent this, you can pass the integer to the method before performing the conversion. The following example uses this method to determine whether the elements in an array of integer values can be converted to `ArrivalStatus` values. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classconversion1.cs" interactive="try-dotnet" id="Snippet7"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classconversion1.fs" id="Snippet7"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classconversion1.vb" id="Snippet7"::: Although the class provides explicit interface implementations of the interface for converting from an enumeration value to an integral type, you should use the methods of the class, such as , to perform these conversions. The following example illustrates how you can use the method along with the method to convert an enumeration value to its underlying type. Note that this example does not require the underlying type of the enumeration to be known at compile time. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classconversion2.cs" id="Snippet8"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classconversion2.fs" id="Snippet8"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classconversion2.vb" id="Snippet8"::: ### Parsing enumeration values - The and methods allow you to convert the string representation of an enumeration value to that value. The string representation can be either the name or the underlying value of an enumeration constant. Note that the parsing methods will successfully convert string representations of numbers that are not members of a particular enumeration if the strings can be converted to a value of the enumeration's underlying type. To prevent this, the method can be called to ensure that the result of the parsing method is a valid enumeration value. The example illustrates this approach and demonstrates calls to both the and methods. Note that the non-generic parsing method returns an object that you may have to cast (in C#) or convert (in Visual Basic) to the appropriate enumeration type. + The and methods allow you to convert the string representation of an enumeration value to that value. The string representation can be either the name or the underlying value of an enumeration constant. Note that the parsing methods will successfully convert string representations of numbers that are not members of a particular enumeration if the strings can be converted to a value of the enumeration's underlying type. To prevent this, the method can be called to ensure that the result of the parsing method is a valid enumeration value. The example illustrates this approach and demonstrates calls to both the and methods. Note that the non-generic parsing method returns an object that you may have to cast (in C# and F#) or convert (in Visual Basic) to the appropriate enumeration type. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classparse1.cs" id="Snippet9"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classparse1.fs" id="Snippet9"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classparse1.vb" id="Snippet9"::: @@ -175,20 +184,23 @@ You can convert enumeration values to their string representations by calling the static method, as well as the overloads of the instance method. You can use a format string to control the precise way in which an enumeration value is represented as a string. For more information, see [Enumeration Format Strings](/dotnet/standard/base-types/enumeration-format-strings). The following example uses each of the supported enumeration format strings ("G" or "g", "D" or "d", "X" or "x", and "F" or "f" ) to convert a member of the `ArrivalStatus` enumeration to its string representations. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classformat1.cs" id="Snippet10"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classformat1.fs" id="Snippet10"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classformat1.vb" id="Snippet10"::: ### Iterating enumeration members - The type does not implement the or interface, which would enable you to iterate members of a collection by using a `foreach` (in C#) or `For Each` (in Visual Basic) construct. However, you can enumerate members in either of two ways. + The type does not implement the or interface, which would enable you to iterate members of a collection by using a `foreach` (in C#), `for..in` (in F#), or `For Each` (in Visual Basic) construct. However, you can enumerate members in either of two ways. - You can call the method to retrieve a string array containing the names of the enumeration members. Next, for each element of the string array, you can call the method to convert the string to its equivalent enumeration value. The following example illustrates this approach. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classiterate.cs" id="Snippet11"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classiterate.fs" id="Snippet11"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classiterate.vb" id="Snippet11"::: - You can call the method to retrieve an array that contains the underlying values in the enumeration. Next, for each element of the array, you can call the method to convert the integer to its equivalent enumeration value. The following example illustrates this approach. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classiterate.cs" id="Snippet12"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classiterate.fs" id="Snippet12"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classiterate.vb" id="Snippet12"::: @@ -198,11 +210,13 @@ In other cases, however, the value of an enumeration object can include multiple enumeration members, and each member represents a bit field in the enumeration value. The attribute can be used to indicate that the enumeration consists of bit fields. For example, an enumeration named `Pets` might be used to indicate the kinds of pets in a household. It can be defined as follows. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classbitwise1.cs" id="Snippet13"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classbitwise1.fs" id="Snippet13"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classbitwise1.vb" id="Snippet13"::: The `Pets` enumeration can then be used as shown in the following example. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classbitwise1.cs" id="Snippet14"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classbitwise1.fs" id="Snippet14"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classbitwise1.vb" id="Snippet14"::: The following best practices should be used when defining a bitwise enumeration and applying the attribute. @@ -218,27 +232,31 @@ - A convenient way to test whether a flag is set in a numeric value is to call the instance method, as shown in the following example. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classbitwise1.cs" id="Snippet15"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classbitwise1.fs" id="Snippet15"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classbitwise1.vb" id="Snippet15"::: It is equivalent to performing a bitwise AND operation between the numeric value and the flag enumerated constant, which sets all bits in the numeric value to zero that do not correspond to the flag, and then testing whether the result of that operation is equal to the flag enumerated constant. This is illustrated in the following example. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classbitwise1.cs" id="Snippet16"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classbitwise1.fs" id="Snippet16"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classbitwise1.vb" id="Snippet16"::: - Use `None` as the name of the flag enumerated constant whose value is zero. You cannot use the `None` enumerated constant in a bitwise AND operation to test for a flag because the result is always zero. However, you can perform a logical, not a bitwise, comparison between the numeric value and the `None` enumerated constant to determine whether any bits in the numeric value are set. This is illustrated in the following example. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/classbitwise1.cs" id="Snippet17"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/classbitwise1.fs" id="Snippet17"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/classbitwise1.vb" id="Snippet17"::: - Do not define an enumeration value solely to mirror the state of the enumeration itself. For example, do not define an enumerated constant that merely marks the end of the enumeration. If you need to determine the last value of the enumeration, check for that value explicitly. In addition, you can perform a range check for the first and last enumerated constant if all values within the range are valid. ## Adding enumeration methods - Because enumeration types are defined by language structures, such as `enum` (C#) and `Enum` (Visual Basic), you cannot define custom methods for an enumeration type other than those methods inherited from the class. However, you can use extension methods to add functionality to a particular enumeration type. + Because enumeration types are defined by language structures, such as `enum` (C#), and `Enum` (Visual Basic), you cannot define custom methods for an enumeration type other than those methods inherited from the class. However, you can use extension methods to add functionality to a particular enumeration type. In the following example, the `Grades` enumeration represents the possible letter grades that a student may receive in a class. An extension method named `Passing` is added to the `Grades` type so that each instance of that type now "knows" whether it represents a passing grade or not. The `Extensions` class also contains a static read-write variable that defines the minimum passing grade. The return value of the `Passing` extension method reflects the current value of that variable. :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/Extensions.cs" interactive="try-dotnet" id="Snippet18"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/Extensions.fs" id="Snippet18"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.class/vb/Extensions.vb" id="Snippet18"::: @@ -248,6 +266,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enummain/CPP/EnumMain.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/Overview/EnumMain.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Overview/EnumMain.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enummain/VB/EnumMain.vb" id="Snippet1"::: ]]> @@ -370,6 +389,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumcompareto/CPP/EnumCompareTo.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/CompareTo/EnumCompareTo.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/CompareTo/EnumCompareTo.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumcompareto/VB/EnumCompareTo.vb" id="Snippet1"::: ]]> @@ -444,11 +464,13 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumequals/CPP/EnumEquals.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/Equals/EnumEquals.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Equals/EnumEquals.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumequals/VB/EnumEquals.vb" id="Snippet1"::: The following example defines two enumeration types, `SledDog` and `WorkDog`. The `SledDog` enumeration has two members, `SledDog.AlaskanMalamute` and `SledDog.Malamute`, that have the same underlying value. The call to the method indicates that these values are equal because their underlying values are the same. The `SledDog.Malamute` and `WorkDog.Newfoundland` members have the same underlying value, although they represent different enumeration types. A call to the method indicates that these values are not equal. :::code language="csharp" source="~/snippets/csharp/System/Enum/Equals/enumequals1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Equals/enumequals1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.equals/vb/enumequals.vb" id="Snippet1"::: ]]> @@ -528,6 +550,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumformat/CPP/EnumFormat.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/Format/EnumFormat.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Format/EnumFormat.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumformat/VB/EnumFormat.vb" id="Snippet1"::: ]]> @@ -662,6 +685,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumgetname/CPP/EnumGetName.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/GetName/EnumGetName.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetName/EnumGetName.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumgetname/VB/EnumGetName.vb" id="Snippet1"::: ]]> @@ -774,6 +798,7 @@ The elements of the return value array are sorted by the binary values of the enumerated constants (that is, by their unsigned magnitude). The following example provides displays information about the array returned by the method for an enumeration that includes a negative, zero, and a positive value. :::code language="csharp" source="~/snippets/csharp/System/Enum/GetNames/getnames1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetNames/getnames1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.getnames/vb/getnames1.vb" id="Snippet1"::: If there are enumerated constants with same value, the order of their corresponding names is unspecified. @@ -785,6 +810,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumgetnames/CPP/EnumGetNames.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/GetNames/EnumGetNames.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetNames/EnumGetNames.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumgetnames/VB/EnumGetNames.vb" id="Snippet1"::: ]]> @@ -945,6 +971,7 @@ The following example calls the method to display the underlying type of some enumeration members. :::code language="csharp" source="~/snippets/csharp/System/Enum/GetUnderlyingType/getunderlyingtype1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetUnderlyingType/getunderlyingtype1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.getunderlyingtype/vb/getunderlyingtype1.vb" id="Snippet1"::: ]]> @@ -1011,6 +1038,7 @@ The elements of the array are sorted by the binary values of the enumeration constants (that is, by their unsigned magnitude). The following example displays information about the array returned by the method for an enumeration that includes a negative value, zero, and a positive value. :::code language="csharp" source="~/snippets/csharp/System/Enum/GetValues/getvalues1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetValues/getvalues1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.getvalues/vb/getvalues1.vb" id="Snippet1"::: The method returns an array that contains a value for each member of the `enumType` enumeration. If multiple members have the same value, the returned array includes duplicate values. In this case, calling the method with each value in the returned array does not restore the unique names assigned to members that have duplicate values. To retrieve all the names of enumeration members successfully, call the method. @@ -1018,11 +1046,13 @@ The method cannot be invoked by using reflection in a reflection-only context. Instead, you can retrieve the value of all enumeration members by using the method to get an array of objects that represent enumeration members and then call the method on each element of the array. The following example illustrates this technique. It requires that you define the following enumeration in an assembly named Enumerations.dll: :::code language="csharp" source="~/snippets/csharp/System/Enum/GetValues/getvalues_reflectiononly.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetValues/getvalues_reflectiononly.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.getvalues/vb/getvalues_reflectiononly.vb" id="Snippet2"::: The assembly is loaded in a reflection-only context, a object that represents the `Pets` enumeration is instantiated, an array of objects is retrieved, and the field values are displayed to the console. :::code language="csharp" source="~/snippets/csharp/System/Enum/GetValues/getvalues_reflectiononly.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetValues/getvalues_reflectiononly.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.getvalues/vb/getvalues_reflectiononly.vb" id="Snippet3"::: @@ -1032,6 +1062,7 @@ :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumgetvalues/CPP/EnumGetValues.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/GetValues/EnumGetValues.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/GetValues/EnumGetValues.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumgetvalues/VB/EnumGetValues.vb" id="Snippet1"::: ]]> @@ -1147,6 +1178,7 @@ thisInstance And flag = flag If the underlying value of `flag` is zero, the method returns `true`. If this behavior is not desirable, you can use the method to test for equality with zero and call only if the underlying value of `flag` is non-zero, as the following example illustrates. :::code language="csharp" source="~/snippets/csharp/System/Enum/HasFlag/hasflag0.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/HasFlag/hasflag0.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.hasflag/vb/hasflag0.vb" id="Snippet1"::: The method is designed to be used with enumeration types that are marked with the attribute and can be used to determine whether multiple bit fields are set. For enumeration types that are not marked with the attribute, call either the method or the method. @@ -1157,6 +1189,7 @@ thisInstance And flag = flag The following example defines an `DinnerItems` enumeration that reflects categories of items that a customer can order in a restaurant. The example tests whether the customer has ordered both an entrée and a beverage. :::code language="csharp" source="~/snippets/csharp/System/Enum/HasFlag/hasflag1.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/HasFlag/hasflag1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.hasflag/vb/hasflag1.vb" id="Snippet2"::: ]]> @@ -1240,6 +1273,7 @@ thisInstance And flag = flag The following example defines an enumeration named `PetType` that consists of individual bit fields. It then calls the method with possible underlying enumeration values, string names, and composite values that result from setting multiple bit fields. :::code language="csharp" source="~/snippets/csharp/System/Enum/IsDefined/isdefined1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/IsDefined/isdefined1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Enum.IsDefined/vb/IsDefined1.vb" id="Snippet1"::: ]]> @@ -1262,6 +1296,7 @@ thisInstance And flag = flag If is an enumeration that is defined by using the attribute, the method returns if multiple bit fields in are set but does not correspond to a composite enumeration value, or if is a string concatenation of the names of multiple bit flags. In the following example, a Pets enumeration is defined with the attribute. The method returns when you pass it an enumeration value that has two bit fields (Pets.Dog and Pets.Cat) set, and when you pass it the string representation of that enumeration value ("Dog, Cat"). :::code language="csharp" source="~/snippets/csharp/System/Enum/IsDefined/isdefined2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/IsDefined/isdefined2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Enum.IsDefined/vb/isdefined2.vb" id="Snippet2"::: You can determine whether multiple bit fields are set by calling the method. @@ -1432,6 +1467,7 @@ thisInstance And flag = flag If `value` is a name that does not correspond to a named constant of `enumType`, the method throws an . If `value` is the string representation of an integer that does not represent an underlying value of the `enumType` enumeration, the method returns an enumeration member whose underlying value is `value` converted to an integral type. If this behavior is undesirable, call the method to ensure that a particular string representation of an integer is actually a member of `enumType`. The following example defines a `Colors` enumeration, calls the method to convert strings to their corresponding enumeration values, and calls the method to ensure that particular integral values are underlying values in the `Colors` enumeration. :::code language="csharp" source="~/snippets/csharp/System/Enum/Parse/ParseExample1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Parse/ParseExample1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Enum.Parse/vb/ParseExample1.vb" id="Snippet1"::: This operation is case-sensitive. @@ -1443,6 +1479,7 @@ thisInstance And flag = flag :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enumparse/CPP/EnumParse.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/Parse/EnumParse.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Parse/EnumParse.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enumparse/VB/EnumParse.vb" id="Snippet1"::: ]]> @@ -1579,6 +1616,7 @@ thisInstance And flag = flag The following example uses the method to parse an array of strings that are created by calling the method. It also uses the method to parse an enumeration value that consists of a bit field. :::code language="csharp" source="~/snippets/csharp/System/Enum/Parse/ParseExample2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/Parse/ParseExample2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Enum.Parse/vb/ParseExample2.vb" id="Snippet2"::: ]]> @@ -3452,6 +3490,7 @@ thisInstance And flag = flag :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_Classic/classic Enum.ToString2 Example/CPP/source.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/ToString/source.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/ToString/source.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_Classic/classic Enum.ToString2 Example/VB/source.vb" id="Snippet1"::: ]]> @@ -3460,11 +3499,13 @@ thisInstance And flag = flag If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value. :::code language="csharp" source="~/snippets/csharp/System/Enum/ToString/tostringbyvalue1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.tostring/vb/tostringbyvalue1.vb" id="Snippet1"::: The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned. :::code language="csharp" source="~/snippets/csharp/System/Enum/ToString/tostringbyvalue1.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.tostring/vb/tostringbyvalue1.vb" id="Snippet2"::: @@ -3588,6 +3629,7 @@ thisInstance And flag = flag :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/enum.tostring/CPP/tostr.cpp" id="Snippet1"::: :::code language="csharp" source="~/snippets/csharp/System/Enum/ToString/tostr.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/ToString/tostr.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/enum.tostring/VB/tostr.vb" id="Snippet1"::: ]]> @@ -3600,11 +3642,13 @@ thisInstance And flag = flag If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value. :::code language="csharp" source="~/snippets/csharp/System/Enum/ToString/tostringbyvalue1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.tostring/vb/tostringbyvalue1.vb" id="Snippet1"::: The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned. :::code language="csharp" source="~/snippets/csharp/System/Enum/ToString/tostringbyvalue1.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/ToString/tostringbyvalue1.fs" id="Snippet3"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.tostring/vb/tostringbyvalue1.vb" id="Snippet3"::: @@ -3979,6 +4023,7 @@ thisInstance And flag = flag The following example defines a `Colors` enumeration, calls the method to convert strings to their corresponding enumeration values, and calls the method to ensure that particular integral values are underlying values in the `Colors` enumeration. :::code language="csharp" source="~/snippets/csharp/System/Enum/TryParseTEnum/tryparse1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/TryParseTEnum/tryparse1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.tryparse/vb/tryparse1.vb" id="Snippet1"::: ]]> @@ -4114,6 +4159,7 @@ thisInstance And flag = flag The following example defines a `Colors` enumeration, calls the method to convert strings to their corresponding enumeration values, and calls the method to ensure that particular integral values are underlying values in the `Colors` enumeration. The method uses case-insensitive comparison when trying to convert the string representations of named constants to their equivalent enumeration values. :::code language="csharp" source="~/snippets/csharp/System/Enum/TryParseTEnum/tryparse2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/Enum/TryParseTEnum/tryparse2.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.enum.tryparse/vb/tryparse2.vb" id="Snippet2"::: ]]>