Skip to content

Commit 8e005e9

Browse files
authored
FlagsAttribute F# snippets (#7726)
1 parent c544fe6 commit 8e005e9

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module flags
2+
3+
//<Snippet1>
4+
open System
5+
6+
// Define an Enum without FlagsAttribute.
7+
type SingleHue =
8+
| None = 0
9+
| Black = 1
10+
| Red = 2
11+
| Green = 4
12+
| Blue = 8
13+
14+
// Define an Enum with FlagsAttribute.
15+
[<Flags>]
16+
type MultiHue =
17+
| None = 0
18+
| Black = 1
19+
| Red = 2
20+
| Green = 4
21+
| Blue = 8
22+
23+
// Display all possible combinations of values.
24+
printfn "All possible combinations of values without FlagsAttribute:"
25+
for i = 0 to 16 do
26+
printfn $"{i,3} - {enum<SingleHue> i:G}"
27+
28+
// Display all combinations of values, and invalid values.
29+
printfn "\nAll possible combinations of values with FlagsAttribute:"
30+
for i = 0 to 16 do
31+
printfn $"{i,3} - {enum<MultiHue> i:G}"
32+
33+
// The example displays the following output:
34+
// All possible combinations of values without FlagsAttribute:
35+
// 0 - None
36+
// 1 - Black
37+
// 2 - Red
38+
// 3 - 3
39+
// 4 - Green
40+
// 5 - 5
41+
// 6 - 6
42+
// 7 - 7
43+
// 8 - Blue
44+
// 9 - 9
45+
// 10 - 10
46+
// 11 - 11
47+
// 12 - 12
48+
// 13 - 13
49+
// 14 - 14
50+
// 15 - 15
51+
// 16 - 16
52+
//
53+
// All possible combinations of values with FlagsAttribute:
54+
// 0 - None
55+
// 1 - Black
56+
// 2 - Red
57+
// 3 - Black, Red
58+
// 4 - Green
59+
// 5 - Black, Green
60+
// 6 - Red, Green
61+
// 7 - Black, Red, Green
62+
// 8 - Blue
63+
// 9 - Black, Blue
64+
// 10 - Red, Blue
65+
// 11 - Black, Red, Blue
66+
// 12 - Green, Blue
67+
// 13 - Black, Green, Blue
68+
// 14 - Red, Green, Blue
69+
// 15 - Black, Red, Green, Blue
70+
// 16 - 16
71+
//</Snippet1>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module flags1
2+
3+
// <Snippet2>
4+
open System
5+
6+
[<Flags>]
7+
type PhoneService =
8+
| None = 0
9+
| LandLine = 1
10+
| Cell = 2
11+
| Fax = 4
12+
| Internet = 8
13+
| Other = 16
14+
15+
// Define three variables representing the types of phone service
16+
// in three households.
17+
let household1 =
18+
PhoneService.LandLine ||| PhoneService.Cell ||| PhoneService.Internet
19+
20+
let household2 =
21+
PhoneService.None
22+
23+
let household3 =
24+
PhoneService.Cell ||| PhoneService.Internet
25+
26+
// Store the variables in a list for ease of access.
27+
let households =
28+
[ household1; household2; household3 ]
29+
30+
// Which households have no service?
31+
for i = 0 to households.Length - 1 do
32+
printfn $"""Household {i + 1} has phone service: {if households[i] = PhoneService.None then "No" else "Yes"}"""
33+
printfn ""
34+
35+
// Which households have cell phone service?
36+
for i = 0 to households.Length - 1 do
37+
printfn $"""Household {i + 1} has cell phone service: {if households[i] &&& PhoneService.Cell = PhoneService.Cell then "Yes" else "No"}"""
38+
printfn ""
39+
40+
// Which households have cell phones and land lines?
41+
let cellAndLand =
42+
PhoneService.Cell ||| PhoneService.LandLine
43+
44+
for i = 0 to households.Length - 1 do
45+
printfn $"""Household {i + 1} has cell and land line service: {if households[i] &&& cellAndLand = cellAndLand then "Yes" else "No"}"""
46+
printfn ""
47+
48+
// List all types of service of each household?//
49+
for i = 0 to households.Length - 1 do
50+
printfn $"Household {i + 1} has: {households[i]:G}"
51+
52+
// The example displays the following output:
53+
// Household 1 has phone service: Yes
54+
// Household 2 has phone service: No
55+
// Household 3 has phone service: Yes
56+
//
57+
// Household 1 has cell phone service: Yes
58+
// Household 2 has cell phone service: No
59+
// Household 3 has cell phone service: Yes
60+
//
61+
// Household 1 has cell and land line service: Yes
62+
// Household 2 has cell and land line service: No
63+
// Household 3 has cell and land line service: No
64+
//
65+
// Household 1 has: LandLine, Cell, Internet
66+
// Household 2 has: None
67+
// Household 3 has: Cell, Internet
68+
// </Snippet2>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="flags.fs" />
9+
<Compile Include="flags1.fs" />
10+
</ItemGroup>
11+
</Project>

xml/System/FlagsAttribute.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
9696
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.FlagsAttribute/CPP/flags.cpp" id="Snippet1":::
9797
:::code language="csharp" source="~/snippets/csharp/System/FlagsAttribute/Overview/flags.cs" interactive="try-dotnet" id="Snippet1":::
98+
:::code language="fsharp" source="~/snippets/fsharp/System/FlagsAttribute/Overview/flags.fs" id="Snippet1":::
9899
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.FlagsAttribute/VB/flags.vb" id="Snippet1":::
99100
100101
The preceding example defines two color-related enumerations, `SingleHue` and `MultiHue`. The latter has the `FlagsAttribute` attribute; the former does not. The example shows the difference in behavior when a range of integers, including integers that do not represent underlying values of the enumeration type, are cast to the enumeration type and their string representations displayed. For example, note that 3 cannot be represented as a `SingleHue` value because 3 is not the underlying value of any `SingleHue` member, whereas the `FlagsAttribute` attribute makes it possible to represent 3 as a `MultiHue` value of `Black, Red`.
@@ -103,6 +104,7 @@
103104
104105
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.FlagsAttribute/CPP/flags1.cpp" id="Snippet2":::
105106
:::code language="csharp" source="~/snippets/csharp/System/FlagsAttribute/Overview/flags1.cs" interactive="try-dotnet" id="Snippet2":::
107+
:::code language="fsharp" source="~/snippets/fsharp/System/FlagsAttribute/Overview/flags1.fs" id="Snippet2":::
106108
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.FlagsAttribute/VB/flags1.vb" id="Snippet2":::
107109
108110
]]></format>
@@ -151,12 +153,14 @@
151153
152154
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.FlagsAttribute/CPP/flags1.cpp" id="Snippet2":::
153155
:::code language="csharp" source="~/snippets/csharp/System/FlagsAttribute/Overview/flags1.cs" interactive="try-dotnet" id="Snippet2":::
156+
:::code language="fsharp" source="~/snippets/fsharp/System/FlagsAttribute/Overview/flags1.fs" id="Snippet2":::
154157
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.FlagsAttribute/VB/flags1.vb" id="Snippet2":::
155158
156159
The following example illustrates the use of the `FlagsAttribute` attribute and shows the effect on the <xref:System.Enum.ToString%2A> method of using `FlagsAttribute` on an <xref:System.Enum> declaration.
157160
158161
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.FlagsAttribute/CPP/flags.cpp" id="Snippet1":::
159162
:::code language="csharp" source="~/snippets/csharp/System/FlagsAttribute/Overview/flags.cs" interactive="try-dotnet" id="Snippet1":::
163+
:::code language="fsharp" source="~/snippets/fsharp/System/FlagsAttribute/Overview/flags.fs" id="Snippet1":::
160164
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.FlagsAttribute/VB/flags.vb" id="Snippet1":::
161165
162166
]]></format>

0 commit comments

Comments
 (0)