Sitemap

C# User Input

2 min readSep 13, 2023

--

#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:

Press enter or click to view image in full size
Input

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:

Press enter or click to view image in full size
Input

The error message will be something like this:

Press enter or click to view image in full size
Error Message

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:

Press enter or click to view image in full size
Casting

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.

--

--

Ömer Faruk Didin
Ömer Faruk Didin

Written by Ömer Faruk Didin

Hi, This is Ömer, I am a 3rd year student of Computer Engineering. I am interested in software and IT. I express my achievements.