Un
Extension method C# per la classe
String che implementa la funzione
IsNullOrWhiteSpace presente dal Framework 4.0.
namespace Sgart
{
public static class Extensions
{
public static bool IsNullOrWhiteSpace(this string str)
{
if (str == null)
{
return true;
}
else if (str == string.Empty)
{
return true;
}
else if (str.Trim().Length == 0)
{
return true;
}
else
{
return false;
}
}
}
}
rispetto al metodo
IsNullOrEmpty della classe string, questo metodo ritorna
true anche se la stringa è composta solo da uno o più spazi.
Un esempio di come usarla:
string test = " ";
if(string.IsNullOrWhiteSpace()){
Console.WriteLine("non valida");
}