Console.WriteLine("insert The Number::");
int c = Convert.ToInt32( Console.ReadLine());

While easy, that will throw an unhandled exceptioon error if the input string can't be converted to an integer. Using the int.TryParse something like this:

        Console.WriteLine("insert The Number::");
        int c = 0;
        while (!int.TryParse(Console.ReadLine(), out c)) 
        {
            Console.WriteLine("Invalid input");
        }

will make your code much less prone to error. c will contain the converted int after the while loop exits.

Edited 3 Years Ago by tinstaafl

Comments
well this is also a way but i told the easiest of all

well this is also a way but i told the easiest of all

Using an easy solution that is also easy to break is a good way to make yourself go bald faster.

Whereas using an easy solution that is hard to break is much more useful.

The easiest solution is not necessarily the best solution. When writing programs you have to consider the "stupid users" factor by making your program as non-breakable as possible. Murphy's Law -- if anythihng can go wrong, it will. Don't expect averyone to enter perfect data -- when testing your program you have to now only test the the right data but also the wrong data. Make an effort to see what breaks your program then make changes to guard against it.

Edited 3 Years Ago by Ancient Dragon

More easy would be:
string c = Console.ReadLine();
Don't tell the user anything and if the user is so bold and dares to input anything else but an integer, just start to laugh!

Edited 3 Years Ago by ddanbe

@Saqlainz, there is no right or wrong in programming, multiple techniques in the end lead to the same result.

Only better/worse practices and more/less efficient techniques.

Edited 3 Years Ago by Mike Askew

This article has been dead for over six months. Start a new discussion instead.