C# TutorialLesson 10Free preview
C# User Input
This lesson is part of C# · All levels
Summary
The program can read user input via Console.ReadLine which always returns the string. Therefore the numbers must be converted to numeric type using int.Parse or int.TryParse. TryParse is better recommended because it returns the value false instead of making the program stop when the input is invalid.
Key points
- Console.ReadLine read one line of input and the result is always string-type.
- It is used to capture one button press, often to hold the screen.
- Parse like int.Parse converts a string to a number but throws an exception when the input is invalid.
- TryParse returns the successful or not bool and fills the result through the out parameter.
- Validate the user input before the calculation so that the program does not crash easily.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.