Mac OS (v12.6)
VS Code (v1.72.0)
dotnet v6.0.401
Simple code:
for (int i = 0; i < 1000; ++i)
{
Console.WriteLine("X");
// Console.Write("X");
}
Good! I see console output.
Ok. Now I try to do the same but with Console.Write
method:
for (int i = 0; i < 1000; ++i)
{
//Console.WriteLine("X");
Console.Write("X");
}
I see nothing now…
Why does Console.Write("X")
method write nothing? How can I solve the problem?
P.S. Console.Out.Flush();
doesn’t help.
P.S.2. If I add Console.WriteLine();
code line after my loop then I get the expected output (thanks to @jmcilhinney). But why I am to add it?
2
Answers
The answer to this is probably in the dotnet/sdk Git Repository at the link below.
https://github.com/dotnet/sdk/issues/5928#issuecomment-215895840
Comment from the GIT Repo
Code that waits for the flush
Basically,
Console.Write();
appends to whatever hasn’t been flushed to the console yet.Console.WriteLine();
is required to flush to the console.This code:
Will produce: "xyz" in the Console
https://learn.microsoft.com/en-us/dotnet/api/system.console.write?view=net-6.0