ReadOnlySpan<T>.GetPinnableReference 方法

定义

返回对可用于固定的 T 类型的对象的只读引用。

此方法旨在支持 .NET 编译器,不应由用户代码调用。

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

返回

T

返回对索引 0 处范围元素的引用;如果范围为空,则为 null

示例

以下示例演示如何创建一个整数数组,将其固定,并将每个元素写入控制台。

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

注解

应用程序不应直接调用 GetPinnableReference。 相反,调用方应使用其语言的普通固定语法,例如 C# 的 fixed 语句。

如果固定 aReadOnlySpan<char>,则 不会 假定结果char*为 null 终止。 此行为不同于固定 a string,其中生成的结果 char* 保证为 null 终止。

适用于