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

DefaultIfNull

return default value if string is null

Source

public static string DefaultIfNull(this string str, string defaultValue)
{
    return str ?? defaultValue;
}

Example

string str = null;
str.DefaultIfNull("I'm nil") // return "I'm nil"

string str1 = "Hello!";
str1.DefaultIfNull("I'm nil") // return "Hello!"

Author: Alexander Gubenko

Submitted on: 22 apr. 2011

Language: C#

Type: System.String

Views: 7891