Lesson 11: Parsing input
We saw that input from Console.ReadLine() is always text. But you can't do math on text: "5" + "3" gives "53", not 8. To compute, you convert the text into a number: int n = int.Parse(text); (or Convert.ToInt32(text)). After the conversion n is a real int you can do math with.
int.Parse is like a translator: it takes the text "5" and turns it into the number 5, so the computer can do math with it.
- int.Parse()
- Converts a string containing a number (like "5") into an int (the number 5).
- Convert.ToInt32()
- Another way to convert a string to an int, similar to int.Parse.
- why parse
- Because arithmetic on strings sticks text; only on numbers does it compute.