ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

IfNullElse

Check if string is null or white spaces and return null alternate value

Source

/// <summary>
/// Check if string is null or white spaces and return a value
/// </summary>
/// <param name="input">string to check</param>
/// <param name="nullAlternateValue">value to be returned in case string is null</param>
/// <returns>string: original or null alternative value</returns>
public static string IfNullElse(this string input, string nullAlternateValue)
{
    return  (!string.IsNullOrWhiteSpace(input)) ? input : nullAlternateValue;
}

Example

string s = "test";
MessageBox.Show(s.IfNullElse("Null Alternate Value"));
s = "";
MessageBox.Show(s.IfNullElse("Null Alternate Value"));

Author: Mohammed Reda Abdulazim

Submitted on: 13 jan. 2019

Language: C#

Type: string

Views: 4298