Simple helper for making things "disposable" I often write small classes to make undisposable things disposable so that .NET can handle something automatically, like stopwatches or logs. I thought why not write a small helper that can be used instead of implementing a full class for simple things.

The Usingifier requries two delegates, one for the initialization and the other one for cleaning-up. (null checks removed for brevity)

class Usingifier&lt;T&gt; : IDisposable
{
    private readonly Func&lt;T&gt; _initialize;
    private readonly Action&lt;T&gt; _cleanUp;
    private T _state;

    public Usingifier(Func&lt;T&gt; initialize, Action&lt;T&gt; cleanUp)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
    }

    public Usingifier&lt;T&gt; Initialize()
    {
        _state = _initialize();
        return this;
    }
    public virtual void Dispose()
    {
        if (_state != null)
        {
            _cleanUp(_state);
        }
    }
}


and here's an example with a Stopwatch

using (var autoStopwatch = new Usingifier&lt;Stopwatch&gt;(() =&gt; Stopwatch.StartNew(), sw =&gt; sw.Elapsed.Dump()))
{
    autoStopwatch.Initialize();
    Thread.Sleep(2000);
}
It's a little difficult to give a full review without the removed null checks, but I'll give an observation:

In Dispose(), you depend on null to be the value of _state to judge whether or not to call the cleanup action. That's all well and good, but you should likely then constrain the generic parameter T to be class

class Usingifier&lt;T&gt; : IDisposable where T : class


You also have an issue with multiple-disposals calling the cleanup action with the state - both probably being stale after the fact. The class should be considered unusable after the cleanup. Or at least unusable until the initialize function has been called again. Simple boolean should take care of that:

class Usingifier&lt;T&gt; : IDisposable where T : class
{
    private readonly Func&lt;T&gt; _initialize;

    private readonly Action&lt;T&gt; _cleanUp;

    private bool _disposed;

    private T _state;

    public Usingifier(Func&lt;T&gt; initialize, Action&lt;T&gt; cleanUp)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
    }

    public Usingifier&lt;T&gt; Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }
    }
}


You may want to also put in some sort of nice (optional) automatic disposal if T itself happens to be IDisposable. Here's a quick take on that:

class Usingifier&lt;T&gt; : IDisposable where T : class
{
    private readonly Func&lt;T&gt; _initialize;

    private readonly Action&lt;T&gt; _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func&lt;T&gt; initialize, Action&lt;T&gt; cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier&lt;T&gt; Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose &amp;&amp; _state is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }


I had a couple more thoughts: perhaps expose _state via a property (can then be accessed within your using scope) and then perhaps extracting an interface for the whole caboodle for mocking purposes during unit testing. Behold:

interface IUsingifier&lt;T&gt; : IDisposable where T : class
{
    T State { get; }

    Usingifier&lt;T&gt; Initialize();
}

class Usingifier&lt;T&gt; : IUsingifier&lt;T&gt; where T : class
{
    private readonly Func&lt;T&gt; _initialize;

    private readonly Action&lt;T&gt; _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    public Usingifier(Func&lt;T&gt; initialize, Action&lt;T&gt; cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public T State { get; private set; }

    public Usingifier&lt;T&gt; Initialize()
    {
        State = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (State != null)
        {
            _cleanUp(State);
        }

        if (_autoDispose &amp;&amp; State is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}