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

IsBoolean()

Tells whether a value can be coalesced into a boolean

Source

public static bool IsBoolean(this string value) {
     var val = value.ToLower().Trim();
     if (val == "false")
          return true;
     if (val == "f")
          return true;
     if (val == "true")
          return true;
     if (val == "t")
          return true;
     if (val == "yes")
          return true;
     if (val == "no")
          return true;
     if (val == "y")
          return true;
     if (val == "n")
          return true;
     
     return false;
}public static bool IsBoolean(this string value) {
     var val = value.ToLower().Trim();
     if (val == "false")
          return false;
     if (val == "f")
          return false;
     if (val == "true")
          return true;
     if (val == "t")
          return true;
     if (val == "yes")
          return true;
     if (val == "no")
          return false;
     if (val == "y")
          return true;
     if (val == "n")
          return false;
     throw new ArgumentException("Value is not a boolean value.");
}

Example

"yes".IsBoolean() //returns true
"n".IsBoolean() //returns true

Author: Anonymous

Submitted on: 25 dec. 2014

Language: C#

Type: String

Views: 6952