Getting Input From User


Getting Input From User -User input is essential for a program to execute its functions and give you the data you want. The .NET Framework has some methods available to you for getting user input from the user. For now, we will discuss the Console.ReadLine() method that gets a string value from the user.

The Console.ReadLine() method takes no arguments and returns a string value which is the value given or typed by the user. As the  name of the method implies, it reads the whole line you type in the console until you press the return or enter key. You can then convert that string data using either the Parse method or the  conversion methods of the Convert class. If the variable that          will store the string data is of type  string, then no conversion is necessary. Let’s take a look at the example program.

using System;

namespace GettingInputDemo
    public class Program
        public static void Main()
            string name;
            int age;
            double height;

            Console.Write("Enter your name: ");
            name = Console.ReadLine();
            Console.Write("Enter your age: ");
            age = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter your height: ");
            height = Convert.ToDouble(Console.ReadLine());

            //Print a blank line

            //Show the details you typed
            Console.WriteLine("Name is {0}.", name);
            Console.WriteLine("Age is {0}.", age);
            Console.WriteLine("Height is {0}.", height);
Enter your name: John
Enter your age: 18
Enter your height: 160.5

Name is John.
Age is 18.
Height is 160.5.

We first declared three variables that will hold the data that we will give to the program. The program prompts the user to enter his name (line 13). Note that we use Console.Write() instead of Console.WriteLine().  Line 14 is where you will get the input from the user. The name variable is assigned the value that is returned by the Console.ReadLine() method.  Since the name is string, and the value returned by Console.ReadLine() is also a string, then no  conversion is necessary. We the prompt for the age of the user. The age is a variable of type  int so we need to do a conversion from string to int. We use Convert.ToInt32() method to do just that. The value returned by that method is what will be assigned to the age variable. Same goes with the height variable but instead, we use Convert.ToDouble() method since height is of type double.

Alternatively, when converting string values to their numerical types, you  can use the Parse() method which is available on every simple type except string.

age = int.Parse(Console.ReadLine());
height = double.Parse(Console.ReadLine());

Note on converting numbers such as int and double from string. The string must or is able to represent a number. If the stringcontains alphanumeric characters or characters which are not numbers and cannot be expressed as a valid number, then an error will occur. For example, the program prompts you for the age and you type your name instead, converting your name to a number will produce an error during runtime.