Instructor Console.Read( ) - is used to read just a single character.
The Read( ) method blocks its return while you
type input characters; it terminates when you press the Enter key. Subsequent calls to the Read( ) method retrieve your input one character at a time. Console.ReadLine( ) - reads input from the console.
Console.ReadLine() is used to read a line
of characters from the standard input stream. When the user presses Enter, it returns a string. Console.ReadLine( ) Though we can assign values to the variables, most of the time we will need to get input from the user. We can use Console.ReadLine( ) for this purpose. Here is a simple example:
string name;
name = Console.ReadLine( );
“ Here, a string variable name is declared. In the next
statement, the Console.ReadLine() will read the value entered by the user and store it in the variable name. ” Console.ReadLine( ) Example: string name; Console.Write(“Enter your name: “); name = Console.ReadLine( );
This is fine for string inputs. However, to input values such
as integers or doubles, we need to adopt a slightly different approach because the resulting input from using Console.ReadLine() is a text input and cannot be assigned to other types of variables. If we try to do this, the program will generate an error. Console.ReadLine( ) One solution to this situation is shown in this example:
int num; Console.Write(“Enter a number: “); num=int.Parse(Console.ReadLine( ));
“ In this case, the resulting input from using Console.ReadLine
is a string but int.Parse( ) will convert it into integer and assign this value to the variable num.”