String - IfEmpty

A string extension method that return the default string passed as parameter if string object is empty, otherwise, orignal string is returned.

Try it

public static void Main()
{
	string [] values = {"EntityFramework", "", "SQL", "Java"};

	foreach(var str in values)
	{
		// C# Extension Method: String - IfEmpty
    	string newStr = str.IfEmpty("Empty String...");
		Console.WriteLine(newStr);
	}
}

View Source
public static partial class Extensions
{
    /// <summary>
    ///     A string extension method that if empty.
    /// </summary>
    /// <param name="value">The value to act on.</param>
    /// <param name="defaultValue">The default value.</param>
    /// <returns>A string.</returns>
    public static string IfEmpty(this string value, string defaultValue)
    {
        return (value.IsNotEmpty() ? value : defaultValue);
    }
}