ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ThrowIf

Throw's a given exception is a given predicate is True

Source

public static T ThrowIf<T>(this T val, Func<T, bool> predicate, Func<Exception> exceptionFunc)
{
    if (predicate(val))
        throw exceptionFunc();
    return val;
}

Example

int? tempId = Submit();

tempId.ThrowIf(i=>!i.HasValue,()=>ServiceFault<FatalExceptionFault>.FaultFor("Unable to submit to Temp Database!"));

Author: Adam

Submitted on: 18 jul. 2013

Language: C#

Type: System.Object

Views: 5731