Span<T>.GetPinnableReference Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Egy T típusú objektumra mutató hivatkozást ad vissza, amely rögzíthető.
Ez a módszer .NET fordítók támogatására szolgál, és nem felhasználói kóddal hívható meg.
public:
T % GetPinnableReference();
public ref T GetPinnableReference();
member this.GetPinnableReference : unit -> 'T
Public Function GetPinnableReference () As T
Válaszok
Hivatkozás a span elemére a 0 indexnél, vagy null ha a span üres.
Példák
Az alábbi példa bemutatja, hogyan hozhat létre egész számtömböt, rögzítheti és írhatja az egyes elemeket a konzolra.
using System;
// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe
public class Example
{
public static unsafe void Main()
{
int[] array = CreateInt32Array();
// Create a span, pin it, and print its elements.
Span<int> span = array.AsSpan();
fixed (int* spanPtr = span)
{
Console.WriteLine($"Span contains {span.Length} elements:");
for (int i = 0; i < span.Length; i++)
{
Console.WriteLine(spanPtr[i]);
}
Console.WriteLine();
}
// Create a read-only span, pin it, and print its elements.
ReadOnlySpan<int> readonlyspan = array.AsSpan();
fixed (int* readonlyspanPtr = readonlyspan)
{
Console.WriteLine($"ReadOnlySpan contains {readonlyspan.Length} elements:");
for (int i = 0; i < readonlyspan.Length; i++)
{
Console.WriteLine(readonlyspanPtr[i]);
}
Console.WriteLine();
}
}
private static int[] CreateInt32Array()
{
return new int[] { 100, 200, 300, 400, 500 };
}
}
// The example displays the following output:
// Span contains 5 elements:
// 100
// 200
// 300
// 400
// 500
//
// ReadOnlySpan contains 5 elements:
// 100
// 200
// 300
// 400
// 500
#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
Megjegyzések
Az alkalmazások nem hívhat GetPinnableReferenceközvetlenül . Ehelyett a hívóknak a nyelvük szokásos rögzítési szintaxisát kell használniuk, például a C#utasítását fixed .
Ha rögzít egy Span<char>értéket, az eredmény char*nem lesz null értékű. Ez a viselkedés eltér a rögzítéstől string, ahol az eredmény char* garantáltan null értékű lesz.