How to check in the first element of array argument send to method is a number in C#

1 Answer

0 votes
using System;

class Program
{
    private static void DisplayArray(string[] arr) {
        string s = arr.Length >= 1 ? arr[0] : throw new ArgumentException("Array is empty");
        
        if (Int64.TryParse(s, out var number))
            Console.WriteLine("Number");
        else
            Console.WriteLine("Not a number");
    }
    static void Main() {
        string[] arr1 = {"c#", "vb.net", "c++"};
        DisplayArray(arr1);
        
        string[] arr2 = {"13", "c", "python"};
        DisplayArray(arr2);
    }
}



/*
run:

Not a number
Number

*/

 



answered Nov 28, 2020 by avibootz
...