DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.0.0-beta.2" />

.NET API 390,616 bytes

 Disposable

public abstract class Disposable : IDisposable
Provides implementation of dispose pattern.
using DotNext.Runtime; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace DotNext { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public abstract class Disposable : IDisposable { private volatile bool disposed; protected bool IsDisposed => disposed; private string ObjectName => GetType().Name; protected Task DisposedTask => Task.FromException(new ObjectDisposedException(ObjectName)); [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void ThrowIfDisposed() { if (IsDisposed) throw new ObjectDisposedException(ObjectName); } protected Task<T> GetDisposedTask<[System.Runtime.CompilerServices.Nullable(2)] T>() { return Task.FromException<T>(new ObjectDisposedException(ObjectName)); } protected bool TrySetDisposedException<[System.Runtime.CompilerServices.Nullable(2)] T>(TaskCompletionSource<T> source) { return source.TrySetException((Exception)new ObjectDisposedException(ObjectName)); } protected bool TrySetDisposedException(TaskCompletionSource source) { return source.TrySetException(new ObjectDisposedException(ObjectName)); } protected virtual void Dispose(bool disposing) { disposed = true; } protected virtual ValueTask DisposeAsyncCore() { Dispose(true); return default(ValueTask); } [AsyncStateMachine(typeof(<DisposeAsyncImpl>d__13))] private ValueTask DisposeAsyncImpl() { <DisposeAsyncImpl>d__13 stateMachine = default(<DisposeAsyncImpl>d__13); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.<>4__this = this; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } protected ValueTask DisposeAsync() { if (!disposed) return DisposeAsyncImpl(); return default(ValueTask); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public static void Dispose([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] IEnumerable<IDisposable> objects) { foreach (IDisposable object in objects) { object?.Dispose(); } } [AsyncStateMachine(typeof(<DisposeAsync>d__17))] public static ValueTask DisposeAsync([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] IEnumerable<IAsyncDisposable> objects) { <DisposeAsync>d__17 stateMachine = default(<DisposeAsync>d__17); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.objects = objects; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public unsafe static void Dispose([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params IDisposable[] objects) { for (IntPtr intPtr = (IntPtr)0; (long)intPtr < (long)Intrinsics.GetLength(objects); intPtr = (IntPtr)(void*)((long)intPtr + 1)) { objects[(long)intPtr]?.Dispose(); } } public static ValueTask DisposeAsync([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params IAsyncDisposable[] objects) { return DisposeAsync(((IEnumerable<IAsyncDisposable>)objects).As()); } ~Disposable() { Dispose(false); } } }