How to check if string is a negative integer in C#

1 Answer

0 votes
using System;
 
class Program
{
    static void Main() {
        string str = "-8";
         
        int n;
         
        if (int.TryParse(str, out n) && n < 0) {
            Console.Write("yes");
        } else {
            Console.Write("no");
        }
         
    }
}
 
 
 
 
/*
run:
 
yes
 
*/

 



answered Jun 25, 2022 by avibootz

Related questions

1 answer 184 views
1 answer 209 views
2 answers 135 views
1 answer 128 views
1 answer 124 views
...