Object - NullIfEquals
A T extension method that null if equals.
public static void Main() { string @this = "1"; // C# Extension Method: Object - NullIfEquals string result1 = @this.NullIfEquals("1"); // return null; string result2 = @this.NullIfEquals("2"); // return "1"; Console.WriteLine(result1); Console.WriteLine(result2); }
View Source
public static partial class Extensions { /// <summary> /// A T extension method that null if equals. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="value">The value.</param> /// <returns>A T.</returns> public static T NullIfEquals<T>(this T @this, T value) where T : class { if (@this.Equals(value)) { return null; } return @this; } }