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

Recommended Answers

All 8 Replies

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.

commented: well this is also a way but i told the easiest of all +0

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.

Will be Working on it!

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!

@ddanbe your post is totally wrong Please correct it!!

Before posting test it ! HUH!

commented: See post below. -1

@saqlainz ok, you have the biggest.(As they say over here) :)

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.