Summary: We always need some utility methods handy to speed up our development activity. The method in this article shows how to check if a string is null or empty.
Introduction: How to create a reusable method to check null or an empty string.
Elaboration:
This Function shows how to pass a String value and check if it is null or empty. This is the most optimized and fastest way to check this. You can also wrap this in a separate utility class.
/// <summary>
/// IsNullOrEmpty method will check for if a string is empty or Null
/// </summary>
/// <param name="stringVar"></param>
/// <returns>returns Boolean</returns>///
public static bool IsNullOrEmpty(string stringVar)
{
bool b = false;
if (stringVar == null || stringVar.Length == 0 || stringVar == " ")
{
b = true;
}
return b;
}
How to use this method?
string UserName = "";
// Check if UserName is empty or null
if (!IsNullOrEmpty(UserName))
{
//Place your code logic when it is not null or empty
}