Class DelegateHelpers
public static class DelegateHelpers
- Inheritance
-
DelegateHelpers
- Inherited Members
Methods
Try(Action)
Tries the execution of a delegate and ignores any exceptions that may occur. Returns true if the delegate was executed successfully, otherwise false if an exception was thrown.
public static bool Try(Action action)
Parameters
actionAction
Returns
Try<T>(Func<T>, T?)
Invokes the specified function and returns its result, or a default value if an exception is thrown.
public static T? Try<T>(Func<T> func, T? defaultValue = default)
Parameters
funcFunc<T>The function to invoke. Cannot be null.
defaultValueTThe value to return if the function throws an exception. The default value for type T is used if not specified.
Returns
- T
The result of the function if it completes successfully; otherwise, the specified default value.
Type Parameters
TThe type of the value returned by the function.
Remarks
This method catches all exceptions thrown by the function and returns the default value instead. Use with caution, as it may suppress unexpected errors.