Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hi Experts

What is the difference between Console.Read() and Console.ReadLine(). Now i am confused with many answers. Can anyone give code example for this, which would great for me to clearly understand the concept.

Help me. I face this question in many interviews. Till now i did not find the correct answer.
Posted
Updated 11-Jun-18 4:04am
v2
Comments
Sergey Alexandrovich Kryukov 9-Dec-13 1:12am    
Is it really so hard to read original MSDN documentation?
By the way, bad interview questions, really bad. Maybe you were lucky not to get in a bad company... :-)
But you certainly need better skills to get a job in a good one... Learning particular APIs does not matter much though...
—SA
Vijaydhas 9-Dec-13 1:25am    
I have attend 29 interviews.. 7 company where ask this question. Coming Saturday i am going to attend 30th interview. For that i preparing for that. May be there also they will ask this same question..!!
Sergey Alexandrovich Kryukov 9-Dec-13 1:34am    
Amazing! So many lousy interviewers. I can imagine what else they could ask. This is the clear indication that these people have no clue on what programming is, but still pretend they do programming. Sad but true: way too typical.

And one advice for you: don't prepare to interviews, this is nearly useless. You will get experience with time, but preparing won't help. Don't waste your time and nerve. Do some development and learning, regardless to interviews. Really, try it. Another method: forget about your desire to get the job, it always helps. Pretend you already got an offer and only discuss the detail. While doing so, focus on understanding what those people will want to get from your help and how can you help them. Really.

—SA
Vijaydhas 9-Dec-13 1:52am    
Sure.. Thank you.. :-)
Jörgen Andersson 9-Dec-13 2:04am    
Where's the upvoting button for comments? This is good advice.

Hi Vijay,
this will make you clear i guess.

C#
class Program
    {
        static void Main(string[] args)
        {
            //Writes the specified string value to the standard output stream (console window)
            Console.Write("All");
            Console.Write(" is");
            Console.Write(" well");
           // output
           // All is well


             //Writes the specified string value, followed by the current line terminator,
            //     to the standard output stream. (console window)
            Console.WriteLine("All");
            Console.WriteLine(" is");
            Console.WriteLine(" well");
            // output
            /*All
             * is
             * well
             */

            Console.Write("test" + Environment.NewLine); // 1) appended a line terminator explicitly
            Console.WriteLine("test"); //2) appends a line terminator implicitly. // (1 & 2) both are same.

            Console.ReadLine();

        }
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Dec-13 1:13am    
If you explained Read and ReadLine... :-)
—SA
Karthik_Mahalingam 9-Dec-13 1:20am    
:-)
Read(), ReadLine() and ReadKey() are basically static methods, and they comes under the Console class. That's why we use these methods like:

Console.Read();
Console.Readline();
Console.ReadKey();

Now Lets see what is the differences in all these methods:

Console.Read():-- method accept the String and return the string as well.

Console.ReadLine():--method accept the String but return Integer.

Finally...

Console.ReadKey():--method accept the Character and also return Character.

That's why we mostly use the Console.ReadKey() method, for come back to source code from output window .

Because when we only press the character we directly come on source code. If you will use the Console.Read() and Console.ReadLine method then
you need to press Enter, come back to the source code rather then any character.
 
Share this answer
 
v2
Comments
BillWoodruff 9-Dec-13 0:29am    
A dump of code is not an answer to a QA question !
Sergey Alexandrovich Kryukov 9-Dec-13 1:20am    
Bill, are you sure you did not mix up answers? I feel you wanted to put this comment and vote on some other answer.
This one does not have a code dump and provides more or less reasonable explanation...
—SA
Vijaydhas 9-Dec-13 1:10am    
But in another website they say like

Console.Readline() accepts the string and returns the string.

Console.Read() accepts the first character of the string and returns ASCII Code(Integer Value) of that first character

Console.ReadKey accepts the Character entered in the command prompt and returns the key info pressed with the modifiers(cntrl,shift,Alt) used while pressing the key.Info like which key pressed,ASCII Code of the Key and Modifiers used and the type of the key which will always be char.
Console.Read() reads only the next character from standard input, and Console.ReadLine() reads the next line of characters from the standard input stream.

check this article
http://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline[^]
http://onlydifferencefaqs.blogspot.com/2012/08/difference-between-readreadline-and.html[^]
 
Share this answer
 
Comments
Vijaydhas 9-Dec-13 0:36am    
Already i refer this both websites. But there is no example. Please give me simple example.
Sergey Alexandrovich Kryukov 9-Dec-13 1:15am    
You asked for it! I'll have to tag your question with "No effort" as you persist with this unfortunate trend.
(But please don't get me wrong: I did not want to discourage you to ask follow-up questions. No effort is mostly related to the original question. Why not reading on this simple topic first?)
—SA
Vijaydhas 9-Dec-13 1:35am    
I read some simple topics.. But i can not get the exact answer to me. After ridoy's answer i found one more reference. http://www.dotnetperls.com/console-read

This link was clear about my doubt..

(Sorry For my poor language..)
Sergey Alexandrovich Kryukov 9-Dec-13 1:38am    
All right. I would advise: next time, read on the topic thoroughly, try to make an evidence to yourself that you understand thing by writing some code and testing it, and ask question only if you are in doubt after that. Not only it can save this site from low-value posts, but will be much more useful for you.
—SA
Vijaydhas 9-Dec-13 1:46am    
Actually I tried myself.

//Read only a single char
int i = Console.Read();
Console.WriteLine(i);

// Read the whole string
sting s = Console.ReadLine();
Console.WriteLine(s);

// Difference between ReadKey() and ReadLine()
//Console.ReadKey();
Console.ReadLine();

I can not get the clear idea.. That's why i ask in this Experts Site..!! And thanks for your advice..!! :-)

Thank you..!!
 
Share this answer
 
v5
Comments
Vijaydhas 9-Dec-13 1:06am    
Thank you.. It's very useful example. Easy to understand. Thanks again for instant reply..!!
ridoy 9-Dec-13 1:10am    
Well, glad to help you.
Sergey Alexandrovich Kryukov 9-Dec-13 1:10am    
Sorry, you messed up a lot here. On Windows, this is #13#10. And #10 is "line feed", #13 is carriage return.
More importantly, CLR is multiplatform, and this end-of-like string depends on platform. The same code with WriteLine will generate different bytes on different platform. So farm you mislead the OP...
—SA
ridoy 9-Dec-13 1:15am    
I say CR is one of the difference btn those 2, and as per Op's reply it is clear he asks it in case of Windows as he marked that as answer later. Moreover, that link cleared him most.
Sergey Alexandrovich Kryukov 9-Dec-13 1:26am    
Well, link is a link, but this is not the excuse for a mistake. And you should not assume it's Windows...
—SA
It is best to understand this by trying in Visual studio. But if you still prefer to read, the shortest way to understand is below :

Console.Read() : Reads the next character from the standard input stream. A static method which accepts the String but returns an Integer

Console.ReadLine() : Reads the next line of characters from the standard input stream. A static method which accepts the String and return the string as well

Hope this helps!
 
Share this answer
 
Comments
Richard MacCutchan 11-Jun-18 11:00am    
Answered more than FOUR years ago.
Nachiket G 11-Jun-18 11:34am    
Haha. Didn't realize its that old.
Just appeared in Quick Answers > View All Questions, so I answered. Nevermind. :)
Richard MacCutchan 11-Jun-18 11:39am    
You need to always look at the date below the question. Some people come here, search for a specific subject just so they can post an 'answer', often in questions more than 5 or 6 years old.
Nachiket G 11-Jun-18 11:44am    
Sure, will remember it for next time. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900