|
|
|
@ -42,5 +42,50 @@ namespace System |
|
|
|
{ |
|
|
|
return list.Contains(item); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Can be used to conditionally perform a function
|
|
|
|
/// on an object and return the modified or the original object.
|
|
|
|
/// It is useful for chained calls.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">An object</param>
|
|
|
|
/// <param name="condition">A condition</param>
|
|
|
|
/// <param name="func">A function that is executed only if the condition is <code>true</code></param>
|
|
|
|
/// <typeparam name="T">Type of the object</typeparam>
|
|
|
|
/// <returns>
|
|
|
|
/// Returns the modified object (by the <paramref name="func"/> if the <paramref name="condition"/> is <code>true</code>)
|
|
|
|
/// or the original object if the <paramref name="condition"/> is <code>false</code>
|
|
|
|
/// </returns>
|
|
|
|
public static T If<T>(this T obj, bool condition, Func<T, T> func) |
|
|
|
{ |
|
|
|
if (condition) |
|
|
|
{ |
|
|
|
return func(obj); |
|
|
|
} |
|
|
|
|
|
|
|
return obj; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Can be used to conditionally perform an action
|
|
|
|
/// on an object and return the original object.
|
|
|
|
/// It is useful for chained calls on the object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">An object</param>
|
|
|
|
/// <param name="condition">A condition</param>
|
|
|
|
/// <param name="action">An action that is executed only if the condition is <code>true</code></param>
|
|
|
|
/// <typeparam name="T">Type of the object</typeparam>
|
|
|
|
/// <returns>
|
|
|
|
/// Returns the original object.
|
|
|
|
/// </returns>
|
|
|
|
public static T If<T>(this T obj, bool condition, Action<T> action) |
|
|
|
{ |
|
|
|
if (condition) |
|
|
|
{ |
|
|
|
action(obj); |
|
|
|
} |
|
|
|
|
|
|
|
return obj; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|