[c#] 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()?

This question is related to c# input console inputstream

The answer is


MSDN is actually pretty clear on this one.

  • Console.Read: Reads the next character from the standard input stream.
  • Console.ReadLine: Reads the next line of characters from the standard input stream.

The difference of Read(),ReadLine() and Readkey() method are given below:

Read():This is static method in Console class:

int i = Console.Read();//it always return int value.
Console.WriteLine(i);

paste above code and give input '1', and the output will be 49. That is Console.Read give int value but that value will be the ASCII value of that.

ReadLine():

string s= Console.ReadLine();//it always return string value.
Console.WriteLine(s);

It gives the string as it is given in the input stream.

ReadKey(): this method is used to hold the output screen.when any key is press. Read() and ReadLine() is used the enter key for exit.


Console.Read() reads just a single character, while Console.ReadLine() reads all characters until the end of line.


Console.Read() is used to read next charater from the standard input stream. When we want to read only the single character then use Console.Read().

Console.ReadLine() is used to read aline of characters from the standard input stream. when we want to read a line of characters use Console.ReadLine().


The basic difference is:

int i = Console.Read();
Console.WriteLine(i);

paste above code and give input 'c', and the output will be 99. That is Console.Read give int value but that value will be the ASCII value of that..

On the other side..

string s = Console.ReadLine();
Console.WriteLine(s);

It gives the string as it is given in the input stream.


Difference between Read(),Readline() and ReadKey() in C#

Read()-Accept the string value and return the string value. Readline() -Accept the string and return Integer ReadKey() -Accept the character and return Character

Summary:

1.The above mentioned three methods are mainly used in Console application and these are used for return the different values . 2.If we use Read line or Read() we need press Enter button to come back to code. 3.If we using Read key() we can press any key to come back code in application


Console.Read()

  • It only accepts a single character from user input and returns its ASCII Code.
  • The data type should be int. because it returns an integer value as ASCII.
  • ex -> int value = Console.Read();

Console.ReadLine()

  • It read all the characters from user input. (and finish when press enter).
  • It returns a String so data type should be String.
  • ex -> string value = Console.ReadLine();

Console.ReadKey()

  • It read that which key is pressed by the user and returns its name. it does not require to press enter key before entering.
  • It is a Struct Data type which is ConsoleKeyInfo.
  • ex -> ConsoleKeyInfo key = Console.ReadKey();

Console.Read()

=> reads only one character from the standard input

Console.ReadLine()

=> reads all characters in the line from the standard input


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).


Console.Read() basically reads a character so if you are on a console and you press a key then the console will close, meanwhile Console.Readline() will read the whole string.


Console.Read() reads a single key, where Console.Readline() waits for the Enter key.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to input

Angular 4 - get input value React - clearing an input value after form submit Min and max value of input in angular4 application Disable Button in Angular 2 Angular2 - Input Field To Accept Only Numbers How to validate white spaces/empty spaces? [Angular 2] Can't bind to 'ngModel' since it isn't a known property of 'input' Mask for an Input to allow phone numbers? File upload from <input type="file"> Why does the html input with type "number" allow the letter 'e' to be entered in the field?

Examples related to console

Error in MySQL when setting default value for DATE or DATETIME Where can I read the Console output in Visual Studio 2015 Chrome - ERR_CACHE_MISS Swift: print() vs println() vs NSLog() Datatables: Cannot read property 'mData' of undefined How do I write to the console from a Laravel Controller? Cannot read property 'push' of undefined when combining arrays Very simple log4j2 XML configuration file using Console and File appender Console.log not working at all Chrome: console.log, console.debug are not working

Examples related to inputstream

Convert InputStream to JSONObject How to read all of Inputstream in Server Socket JAVA Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList? How to create streams from string in Node.Js? How can I read a text file in Android? Can you explain the HttpURLConnection connection process? Open URL in Java to get the content How to convert byte[] to InputStream? Read input stream twice AmazonS3 putObject with InputStream length example