How to Get Input in C# Programming:

The most exciting thing about variables in a computer program is not that they save space, but that they allow the user to input data while using the program! The following would be a code for getting input of various kinds in a console program.



Time To Analyze!!!

So, this program uses a few words that you likely haven't seen before - most notably TryParse. Basically, what TryParse does is it converts whatever you type in (that naturally comes in the string format) into whatever type of variable you specified in its class (eg. Double.TryParse converts the input to a Double). When I write Double.TryParse(Console.ReadLine(), out fingers); what the program does is it takes what you type in, converts it to a double, and then applies it to the variable fingers so we all can know how many fingers that poor man was left with.

But what if you ask in the program "how many fingers does the man have?" and they write in "four and a half" instead of "4.5"? That wouldn't be a double, so it couldn't be assigned to the variable! There is a tricky way to get around this, and it uses the fancy line of code shown below:



So this code adds in a couple more weird things you've never seen before. The exclamation mark and the while statement basically make it so that it will try to TryParse the information, but if its the wrong data type it will display the message below and try again.

Concatenation

Another couple of words you'd probably never heard before are the ToString method and the String.Concat line. Basically, these two lines are just working together to convert everything within the brackets to strings and then write them side by side. String.Concat is the code used to concatenate strings in your output - that means to write them one after another. Be careful when using String.Concat that everything is separated by commas - this is how you tell the program that there are more strings to add on. ToString does exactly what it sounds like: it converts the variables you write in into strings. So why must we use these? Unfortunately, when you give output, it all has to be the same data type. It just so happens that it's easier to convert everything to strings than it is anything else, and thus we change everything to strings.