Disclaimer: I was recently asked this in an interview and quickly found myself dealing with some very messy indexing syntax using arrays. I decided to revisit the problem later to see if my initial hunch of treating the uint input as a string would tend toward a cleaner solution.

/// <summary>
/// Input treated as a string for simplification 
/// </summary>
/// <param name="x"></param>
/// <returns>bool</returns>
protected bool IsPalindrome(uint x)
    string givenNum = Convert.ToString(x);
    char[] input = givenNum.ToCharArray();    

    string testString = String.Empty;
    foreach (char a in input)
        testString += a;

    if (givenNum == testString)
        return true;
        return false;

It works fine but I still thought it was a little more verbose than it could be so I posted this question on Stack Overflow and got some very interesting responses. I eventually went with the following solution; since a uint was specifically specified as a parameter, it would seem that an interviewer would be looking for a numerical, non-string answer.

protected bool IsPalindrome(uint x)
    uint original = x;
    uint reverse = 0;

    while (x > 0)
        reverse *= 10;
        reverse += x % 10;
        x /= 10;
    return original == reverse;

Tags: ,


When uploading a file, you will usually want to check that the extension is of the correct type:

if ((String.Compare(fileExt, ".pdf", true)) == 0) {...}

However, the above code will not work if the letters are in a different case.

This is a better way which ignores the case:

if ((fileExt.IndexOf("pdf", StringComparison.OrdinalIgnoreCase) == 0)) {...}


An even better way (counteracts input such as "pdf2", etc.)

if (string.Equals(fileExt, ".pdf", StringComparison.OrdinalIgnoreCase)) {...}