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

If

Executes a function if a given predicate is true

Source

public static T If<T>(this T val, Func<T,bool> predicate, Func<T, T> func)
{
    if(predicate(val))
    {
        return func(val);
    }
    return val;
}

Example

var lang = GetLanguage();

lang.If(l=>l.Name!="Spanish", l=> MessageBox.Show("Non-spanish language!"));

Author: Adam

Submitted on: 18 jul 2013

Language: C#

Type: System.Object

Views: 3309