ASP.NET, Coding

How to create a reusable method to check null or an empty string

How to create a reusable method to check null or an empty string.

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.

/// 
/// IsNullOrEmpty method will check for if a string is empty or Null
/// 
/// 
/// returns Boolean/// 
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
}

You Might Also Like