How to check if IsNumeric?

If you need to check if something is a number, in C# doesn’t exist a function for that. That I’ve created the following code.

public bool IsNumeric(object Expression)
{
    if (Expression == null || Expression is DateTime)
        return false;

    if (Expression is Int16 || Expression is Int32 || 
        Expression is Int64 || Expression is Decimal || 
        Expression is Single || Expression is Double ||
        Expression is Boolean)
        return true;

    try
    {
        if (Expression is string)
            Double.Parse(Expression as string);
        else
            Double.Parse(Expression.ToString());
        return true;
    }
    catch { } // just dismiss errors but return false
    return false;
}


Happy coding!

Remove line numbers from code

Often you copy from a site some code but it has line numbers and you have to spend some times to remove them. With my simple tool on line, you can remove them quickly.

Try it!

Advertsing

125X125_06





TagCloud

MonthList

CommentList