C# How to check if the user's input is number(not a text or empty)?

Hi! I made a working code, and I stuck with the validation of the user's input. Here is my code(please see the "if" condition under highlighted string): https://pastebin.com/5pRgezSm

10/19/2019 6:06:02 PM

DIY Mods

2 Answers

New Answer


Use Double.TryParse method, it returns true if an expression passed as argument can be safely converted into a Double type. https://docs.microsoft.com/en-us/dotnet/api/system.double.tryparse


Depending on if you mean int or float by "number", you can use these codes(str is user input) : int.TryParse(str) or float.TryParse(str) both statements return true if string is a number, else they'll return false.