Should I Use The IDisposable Dispose Pattern Only For A Disposable Dependency? C#

In the scenario where I'm using a disposable dependency in my type should I just avoid using the IDisposable dispose pattern to avoid the running of the finalizer which will be in effect a useless waste? Example:

class SomeType : IDisposable
{
    private SemaphoreSlim semaphore = new SemaphoreSlim(5);
    public bool IsDisposed{get;private set;}

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                semaphore.Dispose();
            }
            IsDisposed= true;
        }
    }
    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
    ~SomeType()=>Dispose(false);
}

Should I just avoid the Dispose Pattern All together and just implement the single Dispose method in this scenario?



Read more here: https://stackoverflow.com/questions/65713341/should-i-use-the-idisposable-dispose-pattern-only-for-a-disposable-dependency-c

Content Attribution

This content was originally published by AQPBXcI91 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: