TryInvoke Method
Definition
TryInvoke(Action)
Returns a value that indicates whether the specified method can be invoked without an exception.
public static bool TryInvoke(Action method)
Parameters
methodActionThe delegate that to invoke.
Returns
- bool
trueifmethodwas called without raising an exception; otherwisefalse.
Remarks
Actually an anti-pattern in regards to swallowing exception. That said, there are situations where this is a perfectly valid approach.
TryInvoke<TResult>(Func<TResult>, out TResult)
Returns a value that indicates whether the specified method can be invoked without an exception.
public static bool TryInvoke<TResult>(Func<TResult> method, out TResult result)
Parameters
methodFunc<TResult>The function delegate that will resolve
result.resultTResultWhen this method returns, contains the value returned from
method; otherwise the default value for the type of theresultparameter if an exception is thrown.
Returns
- bool
trueif an instance ofTResulthas been created; otherwisefalse.
Type Parameters
TResultThe type of the return value of the
method.
Remarks
Often referred to as the Try-Parse pattern: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions-and-performance