SafeHandleValue Struct

A disposable struct that safely holds a reference to a SafeHandle and its underlying handle value. Automatically increments the reference count on creation and decrements it on disposal.

public readonly struct SafeHandleValue : IDisposable

Remarks

The copies of a value share the reference: disposing any of them releases it, once.

Example

using var safeHandle = File.OpenHandle(Path.GetTempFileName(), FileMode.Open, FileAccess.Read, FileShare.None, FileOptions.DeleteOnClose);

// The SafeHandle cannot be released while the scope is alive
using var scope = safeHandle.CreateHandleScope();
nint handle = scope.Value;
Assert.NotEqual(0, handle); // Use handle safely, for instance in a P/Invoke call
Implements

Constructors

NameDescription
SafeHandleValue(SafeHandle)

SafeHandleValue(SafeHandle)

SafeHandleValue.cs
public SafeHandleValue(SafeHandle safeHandle)

Parameters

safeHandle SafeHandle

No documentation is available.

Properties

NameDescription
HasValueGets a value indicating whether the handle has a valid value.
ValueGets the underlying handle value.

Gets a value indicating whether the handle has a valid value.

public bool HasValue { get; }

Property value

bool

Gets the underlying handle value.

public nint Value { get; }

Property value

nint

Methods

NameDescription
Dispose()
public void Dispose()

No documentation is available.

Operators

NameDescription
implicit operator nint(SafeHandleValue)

implicit operator nint(SafeHandleValue)

SafeHandleValue.cs
public static implicit operator nint(SafeHandleValue handle)

Parameters

handle SafeHandleValue

Returns

nint

No documentation is available.