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

ThrowIfDefault

Throws a given Exception if the given object is equal to the default value for the type

Source

public static T ThrowIfDefault<T>(this T val, Func<Exception> exceptionFunc)
{
    //found here
    //http://www.extensionmethod.net/csharp/object/isdefaultfortype
    if(val.IsDefaultForType())
        throw exceptionFunc();
    return val;
}

Example

var emailData = GetActiveEmailByName(emailName);

emailData.ThrowIfDefault(() => new InvalidOperationException("Unable to load email settings. Is the webconfig key 'EmailName' correct and the record active?"));

Author: Adam

Submitted on: 18 jul 2013

Language: C#

Type: System.Object

Views: 2773