Skip to content

Files

Latest commit

 

History

History
38 lines (29 loc) · 939 Bytes

File metadata and controls

38 lines (29 loc) · 939 Bytes

Pattern: Missing use of ObjectDisposedException

Issue: -

Description

Object checks have a substantial impact on code size and often dominate the code for small functions and property setters. These checks prevent inlining and cause substantial instruction-cache pollution. Throw-helper methods such as ThrowIf are simpler and more efficient than if blocks that construct a new exception instance.

Example of incorrect code:

class TestClass
{
    private bool _disposed = false;
    void TestMethod()
    {
        if (_disposed)
            throw new ObjectDisposedException(GetType().Name);
    }
}

Example of correct code:

class TestClass
{
    private bool _disposed = false;
    void TestMethod()
    {
        ObjectDisposedException.ThrowIf(_disposed, this);
    }
}

Further Reading