Show / Hide Table of Contents

Resolving CA2000: Dispose objects before losing scope

What's the problem?

If you're using Code Analysis to ensure that your code satisfies Microsoft's Framework Design Guidelines, sometimes you may encounter CA2000: Dispose objects before losing scope. There are a number of ways it can occur, but there's one particular cause of this warning that can be resolved by a method in Existential.Net, reducing the amount of code you have to write to do so.

The scenario that Existential.Net can help with is when an object that implements the IDisposable interface has to be returned from the method where it's created. The correct way of doing that is to construct the object using a temporary variable inside a try/finally block before assigning it to the variable that will actually be returned. It's not a very intuitive solution, and takes several lines of code - or you can call an Existential.Net method to do it for you.

How do I resolve it?

The type you want to return must implement IDisposable, and have a default constructor. If it meets those conditions, you can call the method Disposable.SafelyReturn<T>(Action<T>); for example:

public MemoryStream GetMemoryStream()
{
    return Disposable.SafelyReturn<MemoryStream>();
}

The simplest method is to leave the Action<T> parameter unspecified, in which case it will default to null and the default constructor of T will be used.

If further initialisation of the object's needed, an Action that performs the initialisation can be provided, for example:

public MemoryStream GetMemoryStream()
{
    long theLengthInBytes = 256;
    return Disposable.SafelyReturn<MemoryStream>(stream => stream.SetLength(theLengthInBytes));
}
  • Improve this Doc
In This Article
Back to top Copyright © 2021 Dr. Gavin T.D. Greig.