简体   繁体   English

C#中的read和readline有什么区别?

[英]What is the difference between read and readline in C#?

What is the difference between read() and readline() in C#? C#中read()readline()之间有什么区别?

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. 也许我们不使用它,但在我的学院里,唯一的区别是一个人有“线”而另一个没有...在c ++中,有“cin”并且它有“endl”来添加行。 Can somebody tell me the difference? 有人可以告诉我区别吗?

Do you mean TextReader.Read and TextReader.ReadLine ? 你的意思是TextReader.ReadTextReader.ReadLine

One overload of TextReader.Read reads characters into a buffer (a char[] ), and you can specify how many characters you want it to read (as a maximum). TextReader.Read一个重载将TextReader.Read读入缓冲区( char[] ),您可以指定要读取的字符数(作为最大值)。 Another reads a single character, returning an int which will be -1 if you've reached the end of the reader. 另一个读取单个字符,返回一个int ,如果你已经到达读者的末尾将为-1。

TextReader.ReadLine reads a whole line as a string , which doesn't include the line terminator. TextReader.ReadLine将整行读作string ,不包括行终止符。

As far as I'm aware, endl is more commonly used in conjunction with cout in C++: 据我所知, endl更常用于C ++中的cout

cout << "Here's a line" << endl;

In .NET you'd use 在.NET中你会使用

writer.WriteLine("Here's a line")

to accomplish the same thing (for an appropriate TextWriter ; alternatively use Console.WriteLine for the console). 完成相同的事情(对于适当的TextWriter ;或者使用Console.WriteLine作为控制台)。

EDIT: Console.ReadLine reads a line of text, whereas Console.Read reads a single character (it's like the parameterless overload of TextWriter.Read ). 编辑: Console.ReadLine读取一行文本,而Console.Read读取单个字符(它类似于TextWriter.Read的无参数重载)。

Console.ReadLine() is basically the same as Console.In.ReadLine() and Console.Read() is basically the same as Console.In.Read() . Console.ReadLine()是基本相同Console.In.ReadLine()Console.Read()是基本相同Console.In.Read()

EDIT: In answer to your comment to the other answer, you can't do: 编辑:在回答您对其他答案的评论时,您不能这样做:

int x = Console.ReadLine();

because the return type of Console.ReadLine() is a string, and there's no conversion from string to int . 因为Console.ReadLine()的返回类型是一个字符串,并且没有从stringint转换。 You can do 可以做到

int x = Console.Read();

because Console.Read() returns an int . 因为Console.Read()返回一个int (Again, it's the Unicode code point or -1 for "end of data".) (同样,它是Unicode代码点,或者是“数据结束”的-1。)

EDIT: If you want to read an integer from the keyboard, ie the user types in "15" and you want to retrieve that as an integer, you should use something like: 编辑:如果你想从键盘读取一个整数,即用户键入“15”,你想要检索整数,你应该使用类似的东西:

string line = Console.ReadLine();
int value;
if (int.TryParse(line, out value))
{
    Console.WriteLine("Successfully parsed value: {0}", value);
}
else
{
    Console.WriteLine("Invalid number - try again!");
}

If you're talking about Console.Read and Console.ReadLine, the difference is that Read only returns a single character, while ReadLine returns the entire input line. 如果您正在谈论Console.Read和Console.ReadLine,区别在于Read只返回一个字符,而ReadLine返回整个输入行。 Its important to note that in both cases, the API call won't return until the user presses ENTER to submit the text to the program. 重要的是要注意,在这两种情况下,API调用都不会返回,直到用户按ENTER键将文本提交给程序。 So if you type "abc" but don't press ENTER, both Read and ReadLine will block until you do. 因此,如果您键入“abc”但不按ENTER键,Read和ReadLine都会阻塞,直到您执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM