c sharp

How to use isNumeric in C#

Data validation is essential for verifying the precision and dependability of input information in any programming language. The isNumeric function in C# is a useful tool that enables programmers to ascertain whether a string represents a legitimate numeric value. This functionality enables programmers to create robust applications by efficiently validating user input. In this article, we will delve into the isNumeric function in C#, discussing its syntax and providing a comprehensive code example to illustrate its usage.

isNumeric in C#

The isNumeric is not a built-in function or operator in C#. This function is available in Microsoft.VisualBasic namespace, which is not part of the core C# language but can be used in C# code by adding a reference to Microsoft.VisualBasic assembly.

The Information.IsNumeric function in Microsoft.VisualBasic namespace takes a string as input and returns a boolean value indicating whether the input represents a valid numeric value. It gives true if the input is a number; otherwise, it gives false as an output.

Syntax of isNumeric in C#
The isNumeric function is part of Microsoft.VisualBasic namespace can be accessed by referencing that namespace in your C# program. The syntax of the isNumeric function is as follows:

Microsoft.VisualBasic.Information.IsNumeric(expression);

Here, the expression is the value or variable that needs to be evaluated to determine if it represents a valid numeric value. If the expression is numerical, the function gives true in the output; if not, it gives false in the output.

How to Use isNumeric in C#

Suppose a scenario where we want to validate user input for a numeric value. To determine whether a value entered is a number or not, use the isNumeric function. Here is some code that uses isNumeric to show how to do it:

using System;
using Microsoft.VisualBasic;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a numeric value: ");
        string userInput = Console.ReadLine();

        if (Information.IsNumeric(userInput))
        {
            double numericValue = Convert.ToDouble(userInput);
            Console.WriteLine($"You entered a valid numeric value: {numericValue}");
        }
        else
        {
            Console.WriteLine("Invalid input. Please enter a numeric value.");
        }

        Console.ReadLine();
    }
}

The user is prompted to provide a numeric value in the code above, and the userInput variable then contains the entered value. We use the isNumeric function from the Information class to check whether the userInput represents a valid numeric value.

If it does, we use the Convert.ToDouble() method to convert it to a double and display the result. If not, we provide an error notice that says the input is incorrect.

Upon entering an invalid entry, the output will be:

Conclusion

The isNumeric function in C# is a useful tool for data validation, allowing developers to efficiently determine if a given string represents a valid numeric value. By incorporating this function into your code, you can ensure the accuracy and reliability of user input, leading to robust and error-free applications. The provided syntax and code example should serve as a helpful reference to utilize the isNumeric function effectively in your C# projects.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.