Skip to content

Commit 5d52752

Browse files
authored
StackOverflowException F# snippet (#7852)
1 parent 73c2ca5 commit 5d52752

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <Snippet1>
2+
let MAX_RECURSIVE_CALLS = 1000
3+
let mutable ctr = 0
4+
5+
let rec execute () =
6+
ctr <- ctr + 1
7+
if ctr % 50 = 0 then
8+
printfn $"Call number {ctr} to the Execute method"
9+
10+
if ctr <= MAX_RECURSIVE_CALLS then
11+
execute ()
12+
13+
ctr <- ctr - 1
14+
15+
execute ()
16+
printfn $"\nThe call counter: {ctr}"
17+
// The example displays the following output:
18+
// Call number 50 to the Execute method
19+
// Call number 100 to the Execute method
20+
// Call number 150 to the Execute method
21+
// Call number 200 to the Execute method
22+
// Call number 250 to the Execute method
23+
// Call number 300 to the Execute method
24+
// Call number 350 to the Execute method
25+
// Call number 400 to the Execute method
26+
// Call number 450 to the Execute method
27+
// Call number 500 to the Execute method
28+
// Call number 550 to the Execute method
29+
// Call number 600 to the Execute method
30+
// Call number 650 to the Execute method
31+
// Call number 700 to the Execute method
32+
// Call number 750 to the Execute method
33+
// Call number 800 to the Execute method
34+
// Call number 850 to the Execute method
35+
// Call number 900 to the Execute method
36+
// Call number 950 to the Execute method
37+
// Call number 1000 to the Execute method
38+
//
39+
// The call counter: 0
40+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="example1a.fs" />
9+
</ItemGroup>
10+
</Project>

xml/System/StackOverflowException.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ See the [Examples](#examples) section for an illustration of this technique.
6969
The following example uses a counter to ensure that the number of recursive calls to the `Execute` method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant.
7070
7171
:::code language="csharp" source="~/snippets/csharp/System/StackOverflowException/Overview/example1a.cs" interactive="try-dotnet" id="Snippet1":::
72+
:::code language="fsharp" source="~/snippets/fsharp/System/StackOverflowException/Overview/example1a.fs" id="Snippet1":::
7273
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.stackoverflowexception.class/vb/example1a.vb" id="Snippet1":::
7374
7475
]]></format>

0 commit comments

Comments
 (0)