What is the difference between read() and readline() in C#?
Maybe we don’t use it but in my academy the only difference is that one has “line” and the other don’t have…
In c++, there is “cin” and it has “endl” to add line.
Can somebody tell me the difference?
Do you mean
TextReader.ReadandTextReader.ReadLine?One overload of
TextReader.Readreads characters into a buffer (achar[]), and you can specify how many characters you want it to read (as a maximum). Another reads a single character, returning anintwhich will be -1 if you’ve reached the end of the reader.TextReader.ReadLinereads a whole line as astring, which doesn’t include the line terminator.As far as I’m aware,
endlis more commonly used in conjunction withcoutin C++:In .NET you’d use
to accomplish the same thing (for an appropriate
TextWriter; alternatively useConsole.WriteLinefor the console).EDIT:
Console.ReadLinereads a line of text, whereasConsole.Readreads a single character (it’s like the parameterless overload ofTextWriter.Read).Console.ReadLine()is basically the same asConsole.In.ReadLine()andConsole.Read()is basically the same asConsole.In.Read().EDIT: In answer to your comment to the other answer, you can’t do:
because the return type of
Console.ReadLine()is a string, and there’s no conversion fromstringtoint. You can dobecause
Console.Read()returns anint. (Again, it’s the Unicode code point or -1 for “end of data”.)EDIT: If you want to read an integer from the keyboard, i.e. the user types in “15” and you want to retrieve that as an integer, you should use something like: