Skip to content

DisposeAll

Troy Willmot edited this page May 21, 2017 · 3 revisions

DisposeAssistant.DisposeAll is used to dispose multiple objects in a single call. While the TryDispose extension method can be used to dispose many objects in a collection, DisposeAll allows passing each object as it's own parameter.

    // Disposes disposable1, disposable2, and disposable3 independently in a single call.
    // The dispose is performed using [[TryDispose]] so null references/references that don't
    // implement IDisposable are handled quietly.
    DisposeAssistant.DisposeAll(disposable1, disposable2, disposable3);

A second overload exists allowing you to specify options for disposal;

    // Disposes disposable1, disposable2, and disposable3 independently in a single call.
    // The dispose is performed using [[TryDispose]] so null references/references that don't
    // implement IDisposable are handled quietly. Using the SuppressExceptions option 
    // means any errors from the dispose methods will be suppressed.
    DisposeAssistant.DisposeAll(DisposeOptions.SuppressExceptions, disposable1, disposable2, disposable3);

If any errors occur during dispose and DisposeOptions.SuppressExceptions was not specified, a System.AggregateException is thrown containing each inner exception.

Clone this wiki locally