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

  • Question

  • Can someone give me some Example to understand about              

    Difference between Read(),Readline() and ReadKey ?

    I try to find the answer at search... bu it's too many answer then let me confuse.

    So , Please try it easy way that could let me understand....   Thanks !!!


    Junior C Sharp WPF

    Monday, January 9, 2017 7:16 AM

Answers

  • Hi New of C Sharp _Jase,

    Thank you for posting here.

    Console.Read() Method: Reads the next character from the standard input stream.

    It reads in characters from the console. It returns, as an integer, each value until a zero is reached. This signifies the end of the user's input. It should be called in a loop to get all the buffered characters.

    I make a simple example for understanding.

    static void Main(string[] args)
            {
                int i;
                while ((i = Console.Read()) != 0)
                {
                    Console.WriteLine("{0}={1}", i, (char)i);
                }
                Console.ReadKey();
            }


    When I input "hello", here is the output.

    For more details about the Console.Read(), please refer to the MSDN article.

    Console.ReadLine() Method: Reads the next line of characters from the standard input stream.

         string s = Console.ReadLine();
                Console.WriteLine("Your input is {0}.", s);

    When I input "hello", here is the output.

    For more details about the Console.ReadLine(), please refer to the MSDN article.

    Console.ReadKey() Method: Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

    It will wait for keyboard input and exit the program. 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.

    For more details about the Console.ReadKey(), please refer to the MSDN article.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 10, 2017 9:56 AM
    Moderator

All replies

  • Hi,

    1. Console.ReadLine(): A static method which accepts the String and return the string as well.

    2. Console.Read(): A static method which accepts the String but returns an Integer.

    3. Console.ReadKey(): A static method which accepts the Character and return ASCII value of that character. 

    These all three methods Read(), ReadLine() and ReadKey() are basically static methods, and they comes under the Console class. These methods used to accept user input from console screen.

    Monday, January 9, 2017 7:23 AM
    • ReadKey (returns a character): reads only one single character from the standard input stream. 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): reads only single line from the standard input stream. As an example, it can be used to ask the user enter their name or age.
    • Read (returns an int): reads only one single character from the standard input stream. Similar to ReadKey except that it returns an integer.

    This was clearly described with examples in the MSDN documentation here.


    Real men hack kernels!


    • Edited by 0xack13 Monday, January 9, 2017 7:40 AM
    • Proposed as answer by quattuor Monday, January 9, 2017 8:02 AM
    • Unproposed as answer by New of C Sharp _Jase Monday, January 9, 2017 9:06 AM
    Monday, January 9, 2017 7:40 AM
  • Hi New of C Sharp _Jase,

    Thank you for posting here.

    Console.Read() Method: Reads the next character from the standard input stream.

    It reads in characters from the console. It returns, as an integer, each value until a zero is reached. This signifies the end of the user's input. It should be called in a loop to get all the buffered characters.

    I make a simple example for understanding.

    static void Main(string[] args)
            {
                int i;
                while ((i = Console.Read()) != 0)
                {
                    Console.WriteLine("{0}={1}", i, (char)i);
                }
                Console.ReadKey();
            }


    When I input "hello", here is the output.

    For more details about the Console.Read(), please refer to the MSDN article.

    Console.ReadLine() Method: Reads the next line of characters from the standard input stream.

         string s = Console.ReadLine();
                Console.WriteLine("Your input is {0}.", s);

    When I input "hello", here is the output.

    For more details about the Console.ReadLine(), please refer to the MSDN article.

    Console.ReadKey() Method: Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

    It will wait for keyboard input and exit the program. 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.

    For more details about the Console.ReadKey(), please refer to the MSDN article.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 10, 2017 9:56 AM
    Moderator
    • ReadKey (returns a character): reads only one single character from the standard input stream. 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.
    And of course a all-time-favourite: "Press any key to continue". Where you just ReadKey, without bothering about the return value.

    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Tuesday, January 10, 2017 11:45 AM
  • Wendy Zang , can you let me understand "char" ?

    because I'm not learn about "char" yet....


    Junior C Sharp WPF

    Wednesday, January 11, 2017 4:02 AM
  • Hi New of C Sharp _Jase,

    The char I used in the code means convert i form int to char.

     Console.WriteLine("{0}={1}", i, (char)i);

    The program outputs the integer representation and the character representation of each value in the buffer. The final two characters (13 and 10) are equal to the newline.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 11, 2017 9:03 AM
    Moderator