/// <summary> /// Returns the value of <paramref name="source"/> if it is not empty, otherwise throws an exception created from the function provided. /// </summary> /// <typeparam name="T">The sub-type of the Maybe{T} instances.</typeparam> /// <param name="source">The Mabye{T} instance whose value is returned if it is not empty.</param> /// <param name="exceptionFactory">A function that builds the exception to be thrown if <paramref name="source"/> is empty. If the function is null or returns null, a <see cref="System.InvalidOperationException"/> is thrown.</param> /// <returns>The value of <paramref name="source"/>.</returns> public static T ValueOrException <T>(this Maybe <T> source, Func <Exception> exceptionFactory) { if (source.IsEmpty) { throw exceptionFactory?.Invoke() ?? Maybe <T> .CreateEmptyException(); } return(source.Value); }
/// <summary> /// Returns the value of <paramref name="source"/> if it is not empty, otherwise throws the provided exception. /// </summary> /// <typeparam name="T">The sub-type of the Maybe{T} instances.</typeparam> /// <param name="source">The Mabye{T} instance whose value is returned if it is not empty.</param> /// <param name="exception">The exception to throw if <paramref name="source"/> is empty. If null, an <see cref="System.InvalidOperationException"/> is thrown.</param> /// <returns>The value of <paramref name="source"/>.</returns> public static T ValueOrException <T>(this Maybe <T> source, Exception exception) { if (source.IsEmpty) { throw exception ?? Maybe <T> .CreateEmptyException(); } return(source.Value); }