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 문과 같은 해당 언어의 일반 고정 구문을 사용해야 합니다.

고정하는 ReadOnlySpan<char>경우 결과는 char* null 종료로 간주되지 않습니다 . 이 동작은 결과 char* 에서 null 종료가 보장되는 고정string과 다릅니다.

적용 대상