ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Return value of a property must be disposed by the callee

Last modified: 11 February 2024

If you are using the [MustDisposeResourceAttribute] from JetBrains.Annotations to enforce resource disposal in the calling code, ReSharper reports properties that directly return a resource from an annotated source. This means that users of the API must handle a disposable resource that is created each time they use the property. Here is an example:

There are two general ways to fix this problem:

  • Communicate to the API users that the property is not a part of the object. This can be done, for example, by converting the property to a method.

  • Make the property a part of the object and handle the resource internally. This can be done, for example, by adding a backing field for the property and disposing it in the object's Dispose() method.