Home

C# IsNumeric Equivalent

Blog Date 22 July 2021

The Code

using System.Text.RegularExpressions;

    public static Boolean IsNumeric(string fromthis)
    {
        return Regex.IsMatch(fromthis, "^[0-9]+$");
    }

The Explanation

In their perpetually infinite wisdom Microsoft's C# shares many many similarities and functions with their long historied VB. It did not take me long to move over to C# from VB which I had been using for many years.

However one key function did not make the swap from C# to VB. IsNumeric. I have precisely zero idea as to why not. If I were tortured I'd venture a guess that some heathen somewhere expected *real* programmers would be able to write their own code to do this. Pffffft. 

Me? I just wanted to ensure that a particular string only contained numbers and nothing but numbers. IsNumeric did that without any thinking and now MS made me think, thinking hurts.

In the above example I have merely used the simplest of regex examples in a Boolean function. The Regex ensures all characters in the string are numbers. If they are, return true, if there's a random not-a-number character return false.


Got a coding project? Contact ren@techsolus.co.uk

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog