SafeHandleExtensions Class

Provides extension methods for SafeHandle.

public static class SafeHandleExtensions

Methods

NameDescription
CreateHandleScope(SafeHandle)Creates a scope that safely increments and decrements the reference count of a safe handle.

CreateHandleScope(SafeHandle)

SafeHandleExtensions.cs

Creates a scope that safely increments and decrements the reference count of a safe handle.

public static SafeHandleValue CreateHandleScope(this SafeHandle safeHandle)

Parameters

safeHandle SafeHandle

Returns

SafeHandleValue

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