OwnerThisAttribute

Description

sealed class CodeX.OwnerThisAttribute

Derived from

Attribute abstract

The OwnerThis is used on instance methods 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 an instance method / extension method:

    ...
    [OwnerThis]
    void MyObj(...) { ... }
    ...
    [OwnerThis]
    void MyObj(this IDisposable obj, ...) { ... }
    ...

    The instance method takes ownership of the object that is (implicitly passed) to it as this, so the storage container from which the value of this has originated is no longer responsible for disposal.