SafeHandleExtensions Class
Provides extension methods for SafeHandle.
public static class SafeHandleExtensionsMethods
| Name | Description |
|---|---|
| CreateHandleScope(SafeHandle) | Creates a scope that safely increments and decrements the reference count of a safe handle. |
CreateHandleScope(SafeHandle)
SafeHandleExtensions.csCreates a scope that safely increments and decrements the reference count of a safe handle.
public static SafeHandleValue CreateHandleScope(this SafeHandle safeHandle)Parameters
safeHandleSafeHandle
Returns
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