From now and then it happens that I have to databind some entities onto the UI, containing boolean datatypes like "Married" or "Editable" and so on. Printing out "true" or "false" is not really user friendly, so normally what is being done is to have some kind of if clauses like
if (Person.Married == true)
{
return "Yes";
}
else
{
return "No";
}
Obviously this repeats as more people have to do that. So why not create a nice C# extender on boolean types :)
public static class FriendlyBoolAnswerExtender
{
public static string ToFriendlyBoolAnswer(this bool booleanAnswer)
{
if (booleanAnswer)
return "Yes";
else
return "No";
}
}
...and the usage
true.ToFriendlyBoolAnswer();
someObject.Married.ToFriendlyBoolAnswer();
Nothing special actually, but still nice to have and the code gets a bit more elegant and readable :). Of course strings are not returned hardcoded but refactored out
into resource files.
Questions? Thoughts? Hit me up
on Twitter