c# - 当文档建议我不能读取超过 254 个字符时,为什么我可以使用 Console.ReadLine 读取超过 254 个字符?

标签 c# .net .net-core console console.readline

docs对于 Console.ReadLine说:

By default, the method reads input from a 256-character input buffer. Because this includes the Environment.NewLine character(s), the method can read lines that contain up to 254 characters. To read longer lines, call the OpenStandardInput(Int32) method.

但是,我可以阅读更多的字符。运行一个简单的程序,例如:

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

Console.ReadKey();

输入如下:

aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjj

产生长度为 500 的相同输入。

可运行示例 here .

那么这些文档是否已过时,或者“默认”一词是否在这里关键?


更新: Jeremy 发现 source code 中定义的限制为 4096 .

我已经验证 .NET Core 应用程序只会从 stdin 读取前 4094 个字符(不包括换行符)。

就我而言,实际上有一个 .NET Core 3.1 进程启动 .NET Framework 4.6 进程,重定向其 StandardOut 和 StandardIn。我已经通过Console.ReadLine()验证.NET Framework进程可以成功读取10亿个字符。其中 .NET Core 3.1 进程通过 fwProcess.StandardInput.WriteLine(Serialize(<some stuff>)); 发送 Framework 进程内容

当 .NET Framework 进程替换为 .NET Core 进程时,这也适用。

因此,在重定向 stdout/stdin 时,256 个字符的限制似乎并不适用,但如果有人可以找到解释这一点的明确证据/文档,我将不胜感激。我想知道是否仍然有限制(不包括 OutOfMemory 情况),但它是 11 亿个字符。我还想知道这是否依赖于平台(我使用的是 Windows 10)。

如果有帮助,这是我正在运行的代码。

ConsoleApp1:

ProcessStartInfo processInfo = new ProcessStartInfo {
    CreateNoWindow = true,
    FileName = "ConsoleApp2.exe",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardInput = true
};

StringBuilder s = new StringBuilder();
var proc = Process.Start(processInfo);

int n = 1_000_000_000;
for (int i = 0; i < n; i++)
    s.Append("a");

proc.StandardInput.WriteLine(s.ToString());
string s = proc.StandardOutput.ReadLine();
Console.WriteLine(s.Length == n); // logs True

Console.ReadKey();

ConsoleApp2:

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

最佳答案

转述自here :

The default cmd console mode is "ENABLE_LINE_INPUT", which means that when code issues a ::ReadFile call against stdin, ::ReadFile doesn't return to the caller until it encounters a carriage return. But the call to ReadFile only has a limited size buffer that was passed to it. Which means that cmd looks at the buffer size provided to it, and determines based on that how many characters long the line can be... if the line were longer, it wouldn't be able to store all the data into the buffer.

打开 Console.In 时使用的默认缓冲区大小现在为 4096 ( source )

该文档是开源的,并且 available here如果您想提交问题或拉取请求。

重定向 StandardOut/StandardIn 时,此限制不适用。来自 here :

The limit is how much memory you pass in.

关于c# - 当文档建议我不能读取超过 254 个字符时,为什么我可以使用 Console.ReadLine 读取超过 254 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60105591/

相关文章:

c# - 如何将值从 Controller 传递到 asp.net 中查看?

.net - 如何将一个 IntPtr 分配给另一个 IntPtr

c# - 在 Docker 上运行 Asp Core MVC - 静态文件/wwwroot 文件未加载

asp.net-core - EF Core 没有 DBset 上的 .Include() 方法

C#,标志枚举,用于查找标志的通用函数

c# - 使用方法 (C#) 初始化常量变量

c# - 如何比较具有不同 ReflectedType 值的相同 PropertyInfo?

.net - Azure 应用程序服务 Api 应用程序中的 netFrameworkVersion

c# - 如何连接 xaml 和 xaml.cs 文件

c# - 在 .NET Core 2.1 中安装 .NET SQL Client 后,DbProviderFactories.GetFactoryClasses 不返回任何结果