7
Answers

Command Line Argument & int.TryParse()

Maha

Maha

10y
685
1
This following program will check whether value is provided or not in the Command Line Argument, if value is provided whether it is a numeric or not, if it is a numeric whether it is an odd or even.

I have a problem in how trueORfalse = int.TryParse(args[0], out n) behave.

We know inside the if condition if the expression is true that statement becomes o/p.

But in the program if a Command Line Argument is non-numeric though trueORfalse expression is false (Step Into (F11-key) will show the result) statement becomes o/p. How can this situation be explained? Problem is highlighted.

using System;

class Program
{
static void Main(string[] args)
{
int n ;
bool trueORfalse = false;

if(args.Length==0)
Console.WriteLine("Command Line Argument is not provided");
else
if (trueORfalse == int.TryParse(args[0], out n))
Console.WriteLine("Provid a numeric value");
else
if(n%2==0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");

Console.Read();
}
}


Answers (7)