Skip to content

Commit 1cb07a2

Browse files
authored
IndexOutOfRangeException F# snippets (#7771)
1 parent 602402f commit 1cb07a2

File tree

11 files changed

+235
-3
lines changed

11 files changed

+235
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Uninit1
2+
3+
// <Snippet10>
4+
let values1 = [| 3; 6; 9; 12; 15; 18; 21 |]
5+
let values2 = Array.zeroCreate<int> 6
6+
7+
// Assign last element of the array to the new array.
8+
values2[values1.Length - 1] <- values1[values1.Length - 1];
9+
// The example displays the following output:
10+
// Unhandled Exception:
11+
// System.IndexOutOfRangeException:
12+
// Index was outside the bounds of the array.
13+
// at <StartupCode$fs>.main@()
14+
// </Snippet10>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module foreach1
2+
3+
// <Snippet7>
4+
open System
5+
6+
let populateArray items maxValue =
7+
let rnd = Random()
8+
[| for i = 0 to items - 1 do
9+
rnd.Next(0, maxValue + 1) |]
10+
11+
// Generate array of random values.
12+
let values = populateArray 5 10
13+
// Display each element in the array.
14+
for value in values do
15+
printf $"{values[value]} "
16+
17+
// The example displays output like the following:
18+
// 6 4 4
19+
// Unhandled Exception: System.IndexOutOfRangeException:
20+
// Index was outside the bounds of the array.
21+
// at <StartupCode$fs>.main@()
22+
// </Snippet7>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module foreach2
2+
3+
// <Snippet8>
4+
open System
5+
6+
let populateArray items maxValue =
7+
let rnd = Random()
8+
[| for i = 0 to items - 1 do
9+
rnd.Next(0, maxValue + 1) |]
10+
11+
// Generate array of random values.
12+
let values = populateArray 5 10
13+
// Display each element in the array.
14+
for value in values do
15+
printf $"{value} "
16+
17+
// The example displays output like the following:
18+
// 10 6 7 5 8
19+
// </Snippet8>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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="length1.fs" />
9+
<Compile Include="length2.fs" />
10+
<Compile Include="Uninit1.fs" />
11+
<Compile Include="negative1.fs" />
12+
<Compile Include="negative2.fs" />
13+
<Compile Include="nonzero1.fs" />
14+
<Compile Include="nonzero2.fs" />
15+
<Compile Include="foreach1.fs" />
16+
<Compile Include="foreach2.fs" />
17+
</ItemGroup>
18+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module length1
2+
3+
// <Snippet3>
4+
let characters = ResizeArray()
5+
characters.InsertRange(0, [| 'a'; 'b'; 'c'; 'd'; 'e'; 'f' |])
6+
7+
for i = 0 to characters.Count do
8+
printf $"'{characters[i]}' "
9+
// The example displays the following output:
10+
// 'a' 'b' 'c' 'd' 'e' 'f'
11+
// Unhandled Exception:
12+
// System.ArgumentOutOfRangeException:
13+
// Index was out of range. Must be non-negative and less than the size of the collection.
14+
// Parameter name: index
15+
// at <StartupCode$fs>.main@()
16+
// </Snippet3>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module length2
2+
3+
// <Snippet4>
4+
let characters = ResizeArray()
5+
characters.InsertRange(0, [| 'a'; 'b'; 'c'; 'd'; 'e'; 'f' |])
6+
7+
for i = 0 to characters.Count - 1 do
8+
printf $"'{characters[i]}' "
9+
// The example displays the following output:
10+
// 'a' 'b' 'c' 'd' 'e' 'f'
11+
// </Snippet4>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module negative1
2+
3+
// <Snippet5>
4+
open System
5+
6+
let numbers = ResizeArray()
7+
8+
let showValues startValue =
9+
// Create a collection with numeric values.
10+
if numbers.Count = 0 then
11+
numbers.AddRange [| 2..2..22 |]
12+
13+
// Get the index of a startValue.
14+
printfn $"Displaying values greater than or equal to {startValue}:"
15+
let startIndex = numbers.IndexOf startValue
16+
17+
// Display all numbers from startIndex on.
18+
for i = startIndex to numbers.Count - 1 do
19+
printf $" {numbers[i]}"
20+
21+
let startValue =
22+
let args = Environment.GetCommandLineArgs()
23+
if args.Length < 2 then
24+
2
25+
else
26+
match Int32.TryParse args[1] with
27+
| true, v -> v
28+
| _ -> 2
29+
30+
showValues startValue
31+
32+
// The example displays the following output if the user supplies
33+
// 7 as a command-line parameter:
34+
// Displaying values greater than or equal to 7:
35+
//
36+
// Unhandled Exception: System.ArgumentOutOfRangeException:
37+
// Index was out of range. Must be non-negative and less than the size of the collection.
38+
// Parameter name: index
39+
// at System.Collections.Generic.List`1.get_Item(Int32 index)
40+
// at Example.ShowValues(Int32 startValue)
41+
// at <StartupCode$fs>.main@()
42+
// </Snippet5>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module negative2
2+
3+
// <Snippet6>
4+
open System
5+
open System.Collections.Generic
6+
7+
let numbers = new List<int>()
8+
9+
let showValues startValue =
10+
// Create a collection with numeric values.
11+
if numbers.Count = 0 then
12+
numbers.AddRange [| 2..2..22 |]
13+
14+
// Get the index of startValue.
15+
let startIndex = numbers.IndexOf startValue
16+
if startIndex < 0 then
17+
printfn $"Unable to find {startValue} in the collection."
18+
else
19+
// Display all numbers from startIndex on.
20+
printfn $"Displaying values greater than or equal to {startValue}:"
21+
for i = startIndex to numbers.Count - 1 do
22+
printf $" {numbers[i]}"
23+
24+
let startValue =
25+
let args = Environment.GetCommandLineArgs()
26+
if args.Length < 2 then
27+
2
28+
else
29+
match Int32.TryParse args[1] with
30+
| true, v -> v
31+
| _ -> 2
32+
33+
showValues startValue
34+
35+
// The example displays the following output if the user supplies
36+
// 7 as a command-line parameter:
37+
// Unable to find 7 in the collection.
38+
// </Snippet6>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module nonzero1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let values =
7+
Array.CreateInstance(typeof<int>, [| 10 |], [| 1 |])
8+
let mutable value = 2
9+
// Assign values.
10+
for i = 0 to values.Length - 1 do
11+
values.SetValue(value, i)
12+
value <- value * 2
13+
14+
// Display values.
15+
for i = 0 to values.Length - 1 do
16+
printf $"{values.GetValue i} "
17+
18+
// The example displays the following output:
19+
// Unhandled Exception:
20+
// System.IndexOutOfRangeException: Index was outside the bounds of the array.
21+
// at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)
22+
// at System.Array.SetValue(Object value, Int32 index)
23+
// at <StartupCode$fs>.main@()
24+
// </Snippet1>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module nonzero2
2+
3+
// <Snippet2>
4+
open System
5+
6+
let values =
7+
Array.CreateInstance(typeof<int>, [| 10 |], [| 1 |])
8+
let mutable value = 2
9+
// Assign values.
10+
for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
11+
values.SetValue(value, i)
12+
value <- value * 2
13+
14+
// Display values.
15+
for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
16+
printf $"{values.GetValue i} "
17+
// The example displays the following output:
18+
// 2 4 8 16 32 64 128 256 512 1024
19+
// </Snippet2>

0 commit comments

Comments
 (0)