How To Get User Input In C#

1 min read

Console.WriteLine()  is used to output (print) values. Now, let us use the Console.ReadLine()  to get user input.

In the following example, the user can input his or her username, which is stored in the variable userName. Then, we print the value of userName.

Code Sample:

How Can I Get User’s Input With Numbers

The Console.ReadLine()  method returns a string. Therefore, you cannot get information from another data type, such as int. You can solve this problem by using one of the Convert.To  methods.

Code Sample:

Note: If you enter wrong input for example, text in a numerical input, you will get an exception/error message like; System.FormatException:’Input string was not in a correct format.’


Thanks for reading…

Happy Coding!

Leave a Reply

Your email address will not be published.