C# User Input
#9 Greetings, in my last article we talked about “type casting”. Today I will write a short article on “User Input”. Let’s start
Get User Input
We have already learned that Console.WriteLine() is used to output (print) values. Now we will use Console.ReadLine() to get user input.
Get Ömer Faruk Didin’s stories in your inbox
Join Medium for free to get updates from this writer.
In the following example, the user can input his or hers username, which is stored in the variable userName. Then we print the value of userName:
User Input and Numbers
The Console.ReadLine() method returns a string. Therefore, you cannot get information from another data type, such as int. The following program will cause an error:
The error message will be something like this:
Like the error message says, you cannot implicitly convert type ‘string’ to ‘int’.
Luckily, for you, you just learned from the previous article :) (Type Casting), that you can convert any type explicitly, by using one of the Convert.To methods:
Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error message (like System.FormatException: ‘Input string was not in a correct format.’).
This is our article about Input. In the next article we will talk about operators. Stay with the code.








