C# How to check if the user's input is number(not a text or empty)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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

19th Oct 2019, 6:06 PM
DIY Mods
DIY Mods - avatar
2 Answers
+ 2
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
19th Oct 2019, 7:05 PM
Ipang
+ 1
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.
19th Oct 2019, 7:00 PM
ΛM!N
ΛM!N - avatar