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

IsNull

Essentially the implementation of the sql 'isnull' function, allowing the string type (when null) to be replaced with another value.

Source

/// <summary>
	/// If the string is null, converts it to the default string specified. Essentially the same as the SQL IsNull()
	/// function.
	/// </summary>
	/// <param name="inString"></param>
	/// <param name="defaultString"></param>
	/// <returns></returns>
	public static string IsNull(this string inString, string defaultString)
	{
		if (inString==null)
			return defaultString;
		else return inString;
	}

Example

string myString = null;
string aSaferString = myString.IsNull(string.Empty);

Author: Steve Mentzer

Submitted on: 20 okt. 2009

Language: C#

Type: System.String

Views: 5490