ReadOnlySpan<T>.GetPinnableReference Méthode

Définition

Retourne une référence en lecture seule à un objet de type T qui peut être utilisé pour l’épinglage.

Cette méthode est destinée à prendre en charge les compilateurs .NET et n’est pas destinée à être appelée par le code utilisateur.

public:
 T& ^ GetPinnableReference();
public ref readonly T GetPinnableReference ();
member this.GetPinnableReference : unit -> 'T
Public Function GetPinnableReference () As T

Retours

T

Référence à l’élément de l’étendue à l’index 0. Sinon, null si l’étendue est vide.

Exemples

L’exemple suivant montre comment créer un tableau entier, l’épingler et écrire chaque élément dans la console.

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

Remarques

Les applications ne doivent pas appeler GetPinnableReferencedirectement . Au lieu de cela, les appelants doivent utiliser la syntaxe normale d’épinglage de leur langage, comme l’instruction fixed C#.

Si l’épinglage d’un ReadOnlySpan<char>, le résultat n’est char* pas supposé être terminé par null. Ce comportement est différent de l’épinglage d’un , où le résultat char* est garanti d’être terminé par nullstring.

S’applique à