Well it totally depends on what you want as a "clean" result.
If the conversion to string for the reverse bothers you, you can make an extension method to hide it :
public static class Extensions
{
public static int Reverse(this int input)
{
return Convert.ToInt32(string.Concat(input.ToString().Reverse()));
}
}
so the original method is reduced to :
private static bool IsPalindrome(int value)
{
return value == value.Reverse();
}
If you prefer Fluent expressions, you can use .Equals
and make it a one liner :
private static bool IsPalindrome(int value)
{
return string.Concat(value.ToString().Reverse()).Equals(value.ToString());
}
If the duplicate ToString()
calls bothers you, you can put it in a temporary variable :
private static bool IsPalindrome(int value)
{
string valueString = value.ToString();
string inverse = string.Concat(valueString.Reverse());
return inverse == valueString;
}
And of course if you want to leave it like that (and maybe change the var
to string
) it would be correct too, as the original method is truly not horrible.