I've made this extension method. Its purpose is to trigger the disposal of a value, stored in a Nito.AsyncEx AsyncLazy
, as authored by @StephenCleary.
Is this an approach that makes sense? Is the code good?
using System;
using System.Threading.Tasks;
using Nito.AsyncEx;
/// <summary>
/// Extensions to types in the <see cref="Nito.AsyncEx"> namespace.
/// </summary>
public static class NitoAsyncEx
{
/// <summary>
/// Disposes the specified <see cref="AsyncLazy"> value.
/// </summary>
/// <typeparam name="T">The type of the lazy value.</typeparam>
/// <param name="asyncLazy> The asynchronous lazy.</param>
/// <returns>When disposal is complete.</returns>
public static async Task Dispose<T>(
this AsyncLazy<T> asyncLazy) where T : IDisposable
{
if (asyncLazy.Id != 0)
{
(await asyncLazy).Dispose();
}
}
}