Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module arraysegment

// The following code example passes an ArraySegment to a method.

// <Snippet1>
open System

// Print functions.
let printIndexAndValues (myArr: string []) =
for i = 0 to myArr.Length - 1 do
printfn $" [{i}] : {myArr[i]}"
printfn ""

let printIndexAndValuesSeg (arrSeg: ArraySegment<string>) =
for i = arrSeg.Offset to arrSeg.Offset + arrSeg.Count - 1 do
printfn $" [{i}] : {arrSeg.Array[i]}"
printfn ""

// Create and initialize a new string array.
let myArr = [| "The"; "quick"; "brown"; "fox"; "jumps"; "over"; "the"; "lazy"; "dog" |]

// Display the initial contents of the array.
printfn "The original array initially contains:"
printIndexAndValues myArr

// Define an array segment that contains the entire array.
let myArrSegAll = ArraySegment<string>(myArr)

// Display the contents of the ArraySegment.
printfn "The first array segment (with all the array's elements) contains:"
printIndexAndValuesSeg myArrSegAll

// Define an array segment that contains the middle five values of the array.
let myArrSegMid = ArraySegment<string>(myArr, 2, 5)

// Display the contents of the ArraySegment.
printfn "The second array segment (with the middle five elements) contains:"
printIndexAndValuesSeg myArrSegMid

// Modify the fourth element of the first array segment myArrSegAll.
myArrSegAll.Array[3] <- "LION"

// Display the contents of the second array segment myArrSegMid.
// Note that the value of its second element also changed.
printfn "After the first array segment is modified, the second array segment now contains:"
printIndexAndValuesSeg myArrSegMid


(*
This code produces the following output.

The original array initially contains:
[0] : The
[1] : quick
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog

The first array segment (with all the array's elements) contains:
[0] : The
[1] : quick
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog

The second array segment (with the middle five elements) contains:
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the

After the first array segment is modified, the second array segment now contains:
[2] : brown
[3] : LION
[4] : jumps
[5] : over
[6] : the

*)

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="arraysegment.fs" />
<Compile Include="segmentexample.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module segmentexample

// <Snippet2>
open System
open System.Threading.Tasks

// Create array.
let arr = Array.init 50 (fun i -> i + 1)

// Handle array in segments of 10.
let tasks =
Array.chunkBySize 10 arr
|> Array.mapi (fun m elements ->
let mutable segment = ArraySegment<int>(arr, m * 10, elements.Length)
task {
for i = 0 to segment.Count - 1 do
segment[i] <- segment[i] * (m + 1)
} :> Task)

try
Task.WhenAll(tasks).Wait()
let mutable i = 0

for value in arr do
printf $"{value, 3} "
i <- i + 1
if i % 18 = 0 then printfn ""

with :? AggregateException as e ->
printfn "Errors occurred when working with the array:"

for inner in e.InnerExceptions do
printfn $"{inner.GetType().Name}: {inner.Message}"


// The example displays the following output:
// 1 2 3 4 5 6 7 8 9 10 22 24 26 28 30 32 34 36
// 38 40 63 66 69 72 75 78 81 84 87 90 124 128 132 136 140 144
// 148 152 156 160 205 210 215 220 225 230 235 240 245 250
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <Snippet1>
open System

let names =
[| "Adam"; "Bruce"; "Charles"; "Daniel"
"Ebenezer"; "Francis"; "Gilbert"
"Henry"; "Irving"; "John"; "Karl"
"Lucian"; "Michael" |]

let partNames = ArraySegment<string>(names, 2, 5)

// Enumerate over the ArraySegment object.
for part in partNames do
printfn $"{part}"

// The example displays the following output:
// Charles
// Daniel
// Ebenezer
// Francis
// Gilbert
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="example1.fs" />
</ItemGroup>
</Project>
10 changes: 9 additions & 1 deletion xml/System/ArraySegment`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
- For task-based asynchronous operations, you can use an <xref:System.ArraySegment%601> object to ensure that each task operates on a distinct segment of the array. The following example divides an array into individual segments with up to ten elements. Each element in the segment is multiplied by its segment number. The result shows that using the <xref:System.ArraySegment%601> class to manipulate elements in this way changes the values of its underlying array.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/segmentexample.cs" id="Snippet2":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/segmentexample.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/segmentexample.vb" id="Snippet2":::

Note, however, that although the <xref:System.ArraySegment%601> structure can be used to divide an array into distinct segments, the segments are not completely independent of one another. The <xref:System.ArraySegment%601.Array%2A> property returns the entire original array, not a copy of the array; therefore, changes made to the array returned by the <xref:System.ArraySegment%601.Array%2A> property are made to the original array. If this is undesirable, you should perform operations on a copy of the array, rather than an <xref:System.ArraySegment%601> object that represents a portion of the array.
Expand All @@ -111,9 +112,10 @@

- Have the same number of elements.

If you want to retrieve an element by its index in the <xref:System.ArraySegment%601> object, you must cast it to an <xref:System.Collections.Generic.IList%601> object and retrieve it or modify it by using the <xref:System.Collections.Generic.IList%601.Item%2A?displayProperty=nameWithType> property. The following example retrieves the element in an <xref:System.ArraySegment%601> object that delimits a section of a string array.
If you want to retrieve an element by its index in the <xref:System.ArraySegment%601> object, you must cast it to an <xref:System.Collections.Generic.IList%601> object and retrieve it or modify it by using the <xref:System.Collections.Generic.IList%601.Item%2A?displayProperty=nameWithType> property. Note that this is not necessary in F#. The following example retrieves the element in an <xref:System.ArraySegment%601> object that delimits a section of a string array.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.arraysegment.class/cs/example1.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.arraysegment.class/vb/example1.vb" id="Snippet1":::


Expand All @@ -123,6 +125,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::

]]></format>
Expand All @@ -149,6 +152,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -320,6 +324,7 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -498,6 +503,7 @@ The underlying array of <paramref name="destination" /> is <paramref name="null"

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -829,6 +835,7 @@ The underlying array of <paramref name="destination" /> is <paramref name="null"

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.ArraySegment/CPP/arraysegment.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.ArraySegment/CS/arraysegment.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.ArraySegment/FS/arraysegment.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.ArraySegment/VB/arraysegment.vb" id="Snippet1":::

]]></format>
Expand Down Expand Up @@ -1585,6 +1592,7 @@ The underlying array of <paramref name="destination" /> is <paramref name="null"
This member is an explicit interface member implementation. It can be used only when the <xref:System.ArraySegment%601> instance is cast to an <xref:System.Collections.Generic.IList%601> interface, as the following example shows.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.arraysegment.class/cs/example1.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.arraysegment.class/fs/example1.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.arraysegment.class/vb/example1.vb" id="Snippet1":::

]]></format>
Expand Down