Skip to content

Commit e0edde7

Browse files
authored
ReadOnlySpan<T> F# snippets (#7823)
1 parent a8dce7f commit e0edde7

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
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="getpinnablereference1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#nowarn "9"
2+
#nowarn "51"
3+
open System
4+
open FSharp.NativeInterop
5+
6+
let createInt32Array () =
7+
[| 100; 200; 300; 400; 500 |]
8+
9+
[<EntryPoint>]
10+
let main _ =
11+
let array = createInt32Array()
12+
13+
// Create a span, pin it, and print its elements.
14+
let span = array.AsSpan()
15+
let spanPtr = &&span.GetPinnableReference()
16+
printfn $"Span contains {span.Length} elements:"
17+
for i = 0 to span.Length - 1 do
18+
printfn $"{NativePtr.get spanPtr i}"
19+
printfn ""
20+
21+
// Create a read-only span, pin it, and print its elements.
22+
let readonlyspan: ReadOnlySpan<int> = array.AsSpan()
23+
let readonlyspanPtr = &&readonlyspan.GetPinnableReference()
24+
25+
printfn $"ReadOnlySpan contains {readonlyspan.Length} elements:"
26+
for i = 0 to readonlyspan.Length - 1 do
27+
printfn $"{NativePtr.get readonlyspanPtr i}"
28+
printfn ""
29+
0
30+
31+
// The example displays the following output:
32+
// Span contains 5 elements:
33+
// 100
34+
// 200
35+
// 300
36+
// 400
37+
// 500
38+
//
39+
// ReadOnlySpan contains 5 elements:
40+
// 100
41+
// 200
42+
// 300
43+
// 400
44+
// 500

xml/System/ReadOnlySpan`1.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ If pinning a `ReadOnlySpan<char>`, the resulting `char*` __is not__ assumed to b
458458
The following example demonstrates creating an integer array, pinning it, and writing each element to the console.
459459
460460
:::code language="csharp" source="~/snippets/csharp/System/ReadOnlySpanT/GetPinnableReference/getpinnablereference1.cs":::
461+
:::code language="fsharp" source="~/snippets/fsharp/System/ReadOnlySpanT/GetPinnableReference/getpinnablereference1.fs":::
461462
462463
]]></format>
463464
</remarks>

0 commit comments

Comments
 (0)