C# Console Readline and Read

Although in many of the previous tutorial chapters, you must have saw examples using Console.Readline() or Console.Read(), but we will be covering detailed discussion over these methods in this chapter. Basically these two methods are used to take user input in C# console applications. Console.ReadLine(), reads a complete line while C# Console.Read() reads only only the next character from standard input.

Both of the above methods and Console.ReadKey() (Another method to read user input) all are in "System" namespace. Let's read about each of them one by one with example.

C# Console.Read()

Console.Read() reads the next character from the standard input stream in C# console application.

public static int Read ();

It returns the next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

Basically, if you will use Console.Read() and print the output, you will get ASCII value as an output

Example

 public class Program
    {
        public static void Main()
        {
            int i = Console.Read();
            Console.WriteLine(i);
        }
    }

Output:

g
103

c-sharp-console-read-readline-difference-min.png

In the above example, we are reading user input value ( single key value) using Console.Read() which is returning us ASCII value of "g" which is 103 and printed using Console.Writeline().

C# Console.ReadLine()

This method is very useful and used widely to input complete line of characters from user, means it reads the next line of characters from the standard input stream.

public static string ReadLine ();

It returns the next line of characters from the input stream, or null if no more lines are available.

using System;

public class BasicIO
{
	public static void Main()
	{		
		string CompleteString;
		CompleteString = Console.ReadLine();
		Console.WriteLine("Console Readline value= "+CompleteString);
	}
} 

Output

Complete string check
Console Readline value= Complete string check

In the above example, we are trying to read complete line of user input using Console.ReadLine() and printing it.

C# Console.ReadKey()

The ReadKey() method obtains the next character or function key pressed by the user. The pressed key is displayed in the console window. This method is usually used to hold the screen until user press a key.

public static ConsoleKeyInfo ReadKey ();

It returns an object that describes the ConsoleKey constant and Unicode character, if any, that correspond to the pressed console key. The ConsoleKeyInfo object also describes, in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.

Example:

One of the most common uses of the ReadKey() method is to halt program execution until the user presses a key and the app either terminates or displays an additional window of information.

namespace BasicIO
{
    public class BasicIOExample
    {
        public static void Main()
        {
            DateTime dat = DateTime.Now;
            Console.WriteLine("Current Date and time is : "+dat);
            
            Console.Write("Press <Enter> to exit... ");
            while (Console.ReadKey().Key != ConsoleKey.Enter) { }
        }
    }
}

Output:

Current Date and time is : 20/01/2020 18:30:34
Press <Enter> to exit...

In the above example, console application doesn't terminate until user click on "Enter" button.

c-sharp-readline-read-example-min.png


Share Tweet