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);
Create a extension method for string that does something similar to what you specify in the second algorithm, thus cleaning up your code:
public static bool ToBool(this string input)
{
// input will never be null, as you cannot call a method on a null object
if (input.Equals("y", StringComparison.OrdinalIgnoreCase))
{
return true;
}
else if (input.Equals("n", StringComparison.OrdinalIgnoreCase))
{
return false;
}
else
{
throw new Exception("The data is not in the correct format.");
}
}
and call the code:
if (aString.ToBool())
{
// do something
}