Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Console.Read() and Console.ReadLine()?

I'm new to this field and I'm very confused: what is the real difference between Console.Read() and Console.ReadLine()?

like image 613
avirk Avatar asked Jul 26 '11 06:07

avirk


People also ask

What's the difference between console ReadKey (); and console ReadLine ();?

For example, when you use the Console. ReadLine() to input the a string "hello", the program maybe exit and you would not see the output "You input is hello". If you use Console. ReadKey(), the program will wait and would not exit.

What is the difference between read () and ReadLine () function in Python?

Your answer The read() will read the whole file at once and then print out the first characters that take up as many bytes as you specify in the parenthesis versus the readline() that will read and print out only the first characters that take up as many bytes as you specify in the parenthesis.

What is console read ()?

The Console. Read() method in C# is used to read the next character from the standard input stream.

What is difference between console write and console WriteLine?

The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.


2 Answers

Console.Read() reads only the next character from standard input, and Console.ReadLine() reads the next line of characters from the standard input stream.

Standard input in case of Console Application is input from the user typed words in console UI of your application. Try to create it by Visual studio, and see by yourself.

like image 148
VMAtm Avatar answered Oct 12 '22 13:10

VMAtm


These are the methods of system.console

  • ReadKey() (returns a character): reads only one single character from the standard input stream or command line. Usually used when you're giving options to the user in the console to select from, such as select A, B or C. Another prominent example, Press Y or n to continue.
  • ReadLine() (returns a string): or Console.Readline() reads a single line from the standard input stream or the command line. As an example, it can be used to ask the user enter their name or age. It reads all the character till we press enter.
  • Read() (returns an int): or Console.Read() reads only one single character from the standard input stream. Similar to ReadKey except that it returns an integer. It returns the next character from the input stream, or returns (-1) if there is no more character to be read.

(There are more system.console methods like write() and writeline() as well which are used to write in command line, behaving similarly as read() and readline() methods)

This was clearly described with examples in the MSDN documentation (links are included above).

like image 32
0xack13 Avatar answered Oct 12 '22 13:10

0xack13