Skip to content

Commit 66f4ac1

Browse files
authored
System.CharEnumerator F# snippets (dotnet#7536)
1 parent 5d1c631 commit 66f4ac1

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
open System
2+
3+
let useCharEnumerator() =
4+
// <Snippet1>
5+
let title = "A Tale of Two Cities"
6+
let chEnum = title.GetEnumerator()
7+
8+
printfn $"The length of the string is {title.Length} characters:"
9+
10+
let mutable outputLine1 = ""
11+
let mutable outputLine2 = ""
12+
let mutable outputLine3 = ""
13+
let mutable i = 1
14+
15+
while chEnum.MoveNext() do
16+
outputLine1 <- outputLine1 + if i < 10 || i % 10 <> 0 then " " else $"{i / 10} "
17+
outputLine2 <- outputLine2 + $"{i % 10} ";
18+
outputLine3 <- outputLine3 + $"{chEnum.Current} "
19+
i <- i + 1
20+
21+
printfn "%s" outputLine1
22+
printfn "%s" outputLine2
23+
printfn "%s" outputLine3
24+
25+
// The example displays the following output to the console:
26+
// The length of the string is 20 characters:
27+
// 1 2
28+
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
29+
// A T a l e o f T w o C i t i e s
30+
// </Snippet1>
31+
32+
let useForEach () =
33+
// <Snippet2>
34+
let title = "A Tale of Two Cities"
35+
let chEnum = title.GetEnumerator()
36+
37+
printfn $"The length of the string is {title.Length} characters:"
38+
39+
let mutable outputLine1 = ""
40+
let mutable outputLine2 = ""
41+
let mutable outputLine3 = ""
42+
let mutable i = 1
43+
44+
for ch in title do
45+
outputLine1 <- outputLine1 + if i < 10 || i % 10 <> 0 then " " else $"{i / 10} "
46+
outputLine2 <- outputLine2 + $"{i % 10} ";
47+
outputLine3 <- outputLine3 + $"{ch} "
48+
i <- i + 1
49+
50+
printfn "%s" outputLine1
51+
printfn "%s" outputLine2
52+
printfn "%s" outputLine3
53+
54+
// The example displays the following output to the console:
55+
// The length of the string is 20 characters:
56+
// 1 2
57+
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
58+
// A T a l e o f T w o C i t i e s
59+
// </Snippet2>
60+
61+
useCharEnumerator ()
62+
printfn "-----"
63+
useForEach ()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="CharEnumerator1.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/CharEnumerator.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@
9292
9393
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cpp/charenumerator1.cpp" id="Snippet1":::
9494
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cs/CharEnumerator1.cs" id="Snippet1":::
95+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/fs/CharEnumerator1.fs" id="Snippet1":::
9596
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.CharEnumerator.Class/vb/CharEnumerator1.vb" id="Snippet1":::
9697
9798
Note, however, that the same operation can be performed somewhat more intuitively by using `foreach` (in C#) or `For Each` (in Visual Basic), as the following example shows.
9899
99100
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cpp/charenumerator1.cpp" id="Snippet2":::
100101
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cs/CharEnumerator1.cs" id="Snippet2":::
102+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/fs/CharEnumerator1.fs" id="Snippet2":::
101103
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.CharEnumerator.Class/vb/CharEnumerator1.vb" id="Snippet2":::
102104
103105
]]></format>
@@ -223,12 +225,14 @@
223225
224226
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cpp/charenumerator1.cpp" id="Snippet1":::
225227
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cs/CharEnumerator1.cs" id="Snippet1":::
228+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/fs/CharEnumerator1.fs" id="Snippet1":::
226229
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.CharEnumerator.Class/vb/CharEnumerator1.vb" id="Snippet1":::
227230
228231
Note, however, that the same operation can be performed somewhat more intuitively by using `foreach` (in C#) or `For Each` (in Visual Basic), as the following example shows.
229232
230233
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cpp/charenumerator1.cpp" id="Snippet2":::
231234
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cs/CharEnumerator1.cs" id="Snippet2":::
235+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/fs/CharEnumerator1.fs" id="Snippet2":::
232236
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.CharEnumerator.Class/vb/CharEnumerator1.vb" id="Snippet2":::
233237
234238
]]></format>
@@ -342,12 +346,14 @@
342346
343347
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cpp/charenumerator1.cpp" id="Snippet1":::
344348
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cs/CharEnumerator1.cs" id="Snippet1":::
349+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/fs/CharEnumerator1.fs" id="Snippet1":::
345350
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.CharEnumerator.Class/vb/CharEnumerator1.vb" id="Snippet1":::
346351
347352
Note, however, that the same operation can be performed somewhat more intuitively by using `foreach` (in C#) or `For Each` (in Visual Basic), as the following example shows.
348353
349354
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cpp/charenumerator1.cpp" id="Snippet2":::
350355
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/cs/CharEnumerator1.cs" id="Snippet2":::
356+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.CharEnumerator.Class/fs/CharEnumerator1.fs" id="Snippet2":::
351357
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.CharEnumerator.Class/vb/CharEnumerator1.vb" id="Snippet2":::
352358
353359
]]></format>

0 commit comments

Comments
 (0)