Just for neatness sake I was wondering, whether it\'s possible to cast Y or N to a bool? Something like this;
bool theanswer = Convert.ToBoolean(input);
Wonea gave an "IsTrue" source example from DotNetPerls. Here are two shorter versions of it:
public static bool IsTrue(string value)
{
// Avoid exceptions
if (value == null)
return false;
// Remove whitespace from string and lowercase it.
value = value.Trim().ToLower();
return value == "true"
|| value == "t"
|| value == "1"
|| value == "yes"
|| value == "y";
}
OR:
private static readonly IReadOnlyCollection LOWER_TRUE_VALUES = new string[] { "true", "t", "1", "yes", "y" };
public static bool IsTrue(string value)
{
return value != null
? LOWER_TRUE_VALUES.Contains(value.Trim().ToLower())
: false;
}
Heck, if you want to get real short (and ugly), you can collapse that down to two lines like this:
private static readonly IReadOnlyCollection LOWER_TRUE_VALUES = new string[] { "true", "t", "1", "yes", "y" };
public static bool IsTrue(string value) => value != null ? LOWER_TRUE_VALUES.Contains(value.Trim().ToLower()) : false;