OwnerReturnAttribute

Description

sealed class CodeX.OwnerReturnAttribute

Derived from

Attribute abstract

The OwnerReturn is used on return values of methods and properties to declare ownership of disposable objects.

The following rules depict - based on the ownership status - when a disposable object must be disposed and when disposal must not be performed. Going by these rules will avoid situations where a resource is disposed although it is still being used and situations when a resource is never explicitly disposed, thus wasting system resources.

  • When used on a method:

    ...
    [OwnerReturn]
    IDisposable MyObj(...) { ... }
    ...

    The caller takes ownership of the object that is returned by the method and becomes responsible for its disposal.

  • When used on a property:

    ...
    [OwnerReturn]
    IDisposable MyObj { get { ... } }
    ...

    The caller takes ownership of the object that is returned by the property and becomes responsible for its disposal.