Edit

Share via


Unsafe.AsRef Method

Definition

Overloads

AsRef<T>(Void*)

Converts an unmanaged pointer into a managed pointer to a value of type T.

AsRef<T>(T)

Reinterprets the given read-only reference as a mutable reference.

AsRef<T>(Void*)

Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs

Important

This API is not CLS-compliant.

Converts an unmanaged pointer into a managed pointer to a value of type T.

public:
generic <typename T>
 static T % AsRef(void* source);
public static ref T AsRef<T> (void* source);
[System.CLSCompliant(false)]
public static ref T AsRef<T> (void* source);
static member AsRef : nativeptr<unit> -> 'T
[<System.CLSCompliant(false)>]
static member AsRef : nativeptr<unit> -> 'T

Type Parameters

T

The elemental type of the managed pointer.

Parameters

source
Void*

The unmanaged pointer to convert.

Returns

T

A managed pointer to a value of type T.

Attributes

Remarks

The caller is responsible for ensuring that the resulting managed pointer is properly aligned for the referenced type. For more information on alignment requirements, see ECMA-335, Sec. I.12.6.2 ("Alignment").

Applies to

AsRef<T>(T)

Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs

Reinterprets the given read-only reference as a mutable reference.

public:
generic <typename T>
 static T % AsRef(T % source);
public static ref T AsRef<T> (in T source);
public static ref T AsRef<T> (scoped in T source);
public static ref T AsRef<T> (scoped ref T source);
public static ref T AsRef<T> (ref T source);
static member AsRef : 'T -> 'T
Public Shared Function AsRef(Of T) (ByRef source As T) As T

Type Parameters

T

The underlying type of the reference.

Parameters

source
T

The read-only reference to reinterpret.

Returns

T

A mutable reference to a value of type T.

Remarks

This API is conceptually similar to C++'s const_cast<>. It is the caller's responsibility to ensure that no data is written to the referenced location. The runtime contains internal logic predicated on the assumption that readonly references truly are immutable, and callers who violate this invariant may trigger undefined behavior within the runtime.

AsRef is typically used for passing a readonly reference into methods like Add, which accept mutable managed pointers as arguments. Consider the following example.

int ComputeSumOfElements(ref int refToFirstElement, nint numElements)
{
  int sum = 0;
  for (nint i = 0; i < numElements; i++)
  {
    sum += Unsafe.Add(ref refToFirstElement, i);
  }
}

If the input parameter is ref readonly int refToFirstElement instead of ref int refToFirstElement, the previous example will not compile, since readonly references cannot be used as arguments to Add. Instead, AsRef can be used to remove the immutability constraint and allow compilation to succeed, as shown in the following example.

int ComputeSumOfElements(ref readonly int refToFirstElement, nint numElements)
{
  int sum = 0;
  for (nint i = 0; i < numElements; i++)
  {
    sum += Unsafe.Add(ref Unsafe.AsRef(ref refToFirstElement), i);
  }
}

Applies to