C# Extension Method: Object - NullIf

CSharp · Console

/Program.cs

// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002564/object-nullif

// @nuget: Z.ExtensionMethods

using System;			
using System.Linq;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
        string str = "1";

        // C# Extension Method: Object - NullIf
        string result1 = str.NullIf(x => x == "1"); // return null;
        string result2 = str.NullIf(x => x == "2"); // return "1";

        Console.WriteLine(result1);
        Console.WriteLine(result2);
	}
}
;