28 Jan C# User Input
To read input from the console in C#, use the Console.ReadLine() method. In this lesson, we will see an example wherein we have entered the input from the user.
The ReadLine() accepts the input in string format, therefore, we have converted the string to an integer using the Convert.ToInt32() method. Here’s the complete example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace Demo { public class Studyopedia { public static void Main(string[] args) { int i; /* The input entered by the user will be converted to integer using the Convert ToInt32() method */ i = Convert.ToInt32(Console.ReadLine()); // Display the user input Console.WriteLine("i = {0}", i); } } } |
Output
Let’s say the user entered 20, therefore the output will be the following:
1 2 3 |
i = 20 |
No Comments