Object - IfNotNull
A T extension method that execute an action when the value is not null.
public static void Main() { var values = new List<string> { "Fizz", "Buzz" }; List<string> valuesNull = null; string actionOutput1 = null; string actionOutput2 = null; // C# Extension Method: Object - IfNotNull string result1 = values.IfNotNull(x => x.First(), "FizzBuzz"); // return "Fizz"; string result2 = valuesNull.IfNotNull(x => x.First(), "FizzBuzz"); // return "FizzBuzz"; string result3 = valuesNull.IfNotNull(x => x.First(), () => "FizzBuzz"); // return "FizzBuzz" values.IfNotNull(x => actionOutput1 = string.Join("", values)); // = "FizzBuzz" valuesNull.IfNotNull(x => actionOutput2 = string.Join("", values)); // Do Nothing Console.WriteLine(result1); Console.WriteLine(result2); Console.WriteLine(result3); Console.WriteLine(actionOutput1); Console.WriteLine(actionOutput2); }
View Source
using System; public static partial class Extensions { /// <summary>A T extension method that execute an action when the value is not null.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="action">The action.</param> public static void IfNotNull<T>(this T @this, Action<T> action) where T : class { if (@this != null) { action(@this); } } /// <summary> /// A T extension method that the function result if not null otherwise default value. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <returns>The function result if @this is not null otherwise default value.</returns> public static TResult IfNotNull<T, TResult>(this T @this, Func<T, TResult> func) where T : class { return @this != null ? func(@this) : default(TResult); } /// <summary> /// A T extension method that the function result if not null otherwise default value. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The function result if @this is not null otherwise default value.</returns> public static TResult IfNotNull<T, TResult>(this T @this, Func<T, TResult> func, TResult defaultValue) where T : class { return @this != null ? func(@this) : defaultValue; } /// <summary> /// A T extension method that the function result if not null otherwise default value. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="func">The function.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>The function result if @this is not null otherwise default value.</returns> public static TResult IfNotNull<T, TResult>(this T @this, Func<T, TResult> func, Func<TResult> defaultValueFactory) where T : class { return @this != null ? func(@this) : defaultValueFactory(); } }