Table of Contents

Defer Method

Defer<T>(T, Action<T>)

public static Defer.DeferredDisposable<T> Defer<T>(this T self, Action<T> act) where T : class

Always execute action when exiting method or block scope even if fault by exception.

Parameters

self T
act Action<T>

Returns

Defer.DeferredDisposable<T>

Disposable struct for using statement.

Type Parameters

T

Remarks

DO declare lambda as static if possible. It will reuse existing instance. ex: .Defer(static x => {...});
DON'T pass static method directly. C# 9.0 will allocate Action<T> on every call. ex: .Defer(MyStaticMethod);

Defer<T>(ref T, Action<T>) Deprecated

[Obsolete("Value type is copied into `DeferredAction<T>` instance and callback is invoked with it. (ie. could have old value) Consider using `this.Defer(x => x._myValueTypeAsField)` instead to take updated value in callback.")]
public static Defer.DeferredDisposable<T> Defer<T>(this ref T self, Action<T> act) where T : struct

Always execute action when exiting method or block scope even if fault by exception.

Parameters

self T
act Action<T>

Returns

Defer.DeferredDisposable<T>

Disposable struct for using statement.

Type Parameters

T

Remarks

DO declare lambda as static if possible. It will reuse existing instance. ex: .Defer(static x => {...});
DON'T pass static method directly. C# 9.0 will allocate Action<T> on every call. ex: .Defer(MyStaticMethod);

Defer<T, TState>(T, TState, Action<T, TState>)

public static Defer.DeferredDisposable<T, TState> Defer<T, TState>(this T self, TState state, Action<T, TState> act) where T : class

Always execute action when exiting method or block scope even if fault by exception.

Parameters

self T
state TState
act Action<T, TState>

Returns

Defer.DeferredDisposable<T, TState>

Disposable struct for using statement.

Type Parameters

T
TState

Remarks

DO declare lambda as static if possible. It will reuse existing instance. ex: .Defer(static x => {...});
DON'T pass static method directly. C# 9.0 will allocate Action<T> on every call. ex: .Defer(MyStaticMethod);

Defer<T, TState>(ref T, TState, Action<T, TState>) Deprecated

[Obsolete("Value type is copied into `DeferredAction<T>` instance and callback is invoked with it. (ie. could have old value) Consider using `this.Defer(x => x._myValueTypeAsField)` instead to take updated value in callback.")]
public static Defer.DeferredDisposable<T, TState> Defer<T, TState>(this ref T self, TState state, Action<T, TState> act) where T : struct

Always execute action when exiting method or block scope even if fault by exception.

Parameters

self T
state TState
act Action<T, TState>

Returns

Defer.DeferredDisposable<T, TState>

Disposable struct for using statement.

Type Parameters

T
TState

Remarks

DO declare lambda as static if possible. It will reuse existing instance. ex: .Defer(static x => {...});
DON'T pass static method directly. C# 9.0 will allocate Action<T> on every call. ex: .Defer(MyStaticMethod);