An example of how you could do it with
" tryParse ".
The code could be even smaller by removing the method
" checkString(string str) "
and replacing it in the if statement with
a TryParse.
static void Main(string[] args)
{
string[] strArray = {"100k", "10", "10k" };
string strPlaceholder;
Console.WriteLine("\n ~~ Press F12 to quit ~~\n");
do
{
foreach (string str in strArray)
{
strPlaceholder = null;
if (!checkString(str))
strPlaceholder = "not";
Console.WriteLine($"# {str} is {strPlaceholder} a valid Number");
}
} while (Console.ReadKey(true).Key != ConsoleKey.F12);
}
static bool checkString(string str)
{
int placeholder;
return int.TryParse(str, out placeholder);
}