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,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="getpinnablereference1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#nowarn "9"
#nowarn "51"
open System
open FSharp.NativeInterop

let createInt32Array () =
[| 100; 200; 300; 400; 500 |]

[<EntryPoint>]
let main _ =
let array = createInt32Array()

// Create a span, pin it, and print its elements.
let span = array.AsSpan()
let spanPtr = &&span.GetPinnableReference()
printfn $"Span contains {span.Length} elements:"
for i = 0 to span.Length - 1 do
printfn $"{NativePtr.get spanPtr i}"
printfn ""

// Create a read-only span, pin it, and print its elements.
let readonlyspan: ReadOnlySpan<int> = array.AsSpan()
let readonlyspanPtr = &&readonlyspan.GetPinnableReference()

printfn $"ReadOnlySpan contains {readonlyspan.Length} elements:"
for i = 0 to readonlyspan.Length - 1 do
printfn $"{NativePtr.get readonlyspanPtr i}"
printfn ""
0

// The example displays the following output:
// Span contains 5 elements:
// 100
// 200
// 300
// 400
// 500
//
// ReadOnlySpan contains 5 elements:
// 100
// 200
// 300
// 400
// 500
1 change: 1 addition & 0 deletions xml/System/ReadOnlySpan`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ If pinning a `ReadOnlySpan<char>`, the resulting `char*` __is not__ assumed to b
The following example demonstrates creating an integer array, pinning it, and writing each element to the console.

:::code language="csharp" source="~/snippets/csharp/System/ReadOnlySpanT/GetPinnableReference/getpinnablereference1.cs":::
:::code language="fsharp" source="~/snippets/fsharp/System/ReadOnlySpanT/GetPinnableReference/getpinnablereference1.fs":::

]]></format>
</remarks>
Expand Down