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

NullStringToEmptyString

If input is null, returns empty string. Otherwise returns original input. After using this method, you can use all string methods without the danger of null exception.

Source

public static string NullStringToEmptyString(this string input)
{
	if (input == null)
	{
		return string.Empty;
	}

	return input;
}

Example

string myString = null;
myString = myString.NullStringToEmptyString();
myString = myString.ToLower();// some operation with no need for null check

Author: Joseph K

Submitted on: 3 nov. 2014

Language: C#

Type: string

Views: 4651