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

IsNullThenEmpty

A handy extension method for System.String that eliminates this pattern when trying to avoid null reference exceptions. if (someString==null) someString=string.Empty;

Source

/// <summary>
	/// if the string is NULL, converts it to string.empty. Helpful when trying to avoid null conditions.
	/// </summary>
	/// <param name="inString"></param>
	/// <returns></returns>
	public static string IsNullThenEmpty(this string inString)
	{
		if (inString==null)
			return string.Empty;
		else
			return inString;
	}

Example

string theUserData = someTextBox.Text.IsNullThenEmpty();

Author: Steve Mentzer

Submitted on: 20 okt. 2009

Language: C#

Type: System.String

Views: 4953