Console.WriteLine prints text to the console followed by a line break. Console.Write prints the same text without the line break, so the next output continues on the same line. In a new .NET console project, this is the whole program:
Console.WriteLine("Hello, World!");
Since .NET 6, the console template uses top-level statements: no namespace, no Program class and no Main method to write. The compiler generates them. Implicit usings import System, so Console is available without a using line.
Write versus WriteLine
Console.Write("Hello, ");
Console.WriteLine("World!");
Console.WriteLine();
Console.Write("Loading");
Console.Write(".");
Console.Write(".");
Console.WriteLine(" done");
Output:
Hello, World!
Loading.. done
Console.WriteLine() with no arguments writes an empty line. Both methods have overloads for every primitive type, so Console.WriteLine(42) or Console.WriteLine(true) works without converting to a string first.
Write values into the text
String interpolation is the usual way to mix text and values. The older composite format with numbered placeholders still works and appears in a lot of existing code:
string name = "Ada";
int unread = 3;
Console.WriteLine($"Hi {name}, you have {unread} new messages.");
Console.WriteLine("Hi {0}, you have {1} new messages.", name, unread);
Output:
Hi Ada, you have 3 new messages.
Hi Ada, you have 3 new messages.
Numbers and dates are formatted with the current culture, so {price:N2} prints 1,234.50 on an English system and 1.234,50 on a German one. See the formatting examples linked below for format strings and how to fix the culture.
Errors go to standard error
A console app has two output streams. Console.Out (what Console.WriteLine uses) is standard output; Console.Error is standard error. Write error messages to the second one:
Console.Error.WriteLine("Could not open settings.json");
Both appear in the terminal, but they are separate when someone redirects output. myapp > result.txt captures only standard output, so the error message still shows up on screen instead of being buried in the file. Scripts and CI tools rely on that separation.
Colors
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Warning: disk space is low");
Console.ResetColor();
The color stays set until you change it, so call ResetColor() afterwards, preferably in a finally block if code in between can throw. The 16 ConsoleColor values work in Windows Terminal, the classic console and Linux and macOS terminals. When output is redirected to a file, colors are simply ignored.
Non-English characters
If accented letters or symbols come out as ? on Windows, the console is using a legacy code page. Switch it to UTF-8 at the start of the program:
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("Grüße, €100");
Linux and macOS terminals use UTF-8 already. The font of the terminal also has to contain the characters; emoji in particular depend on it.
Large outputs
Every Console.WriteLine call takes a lock and, by default, flushes to the terminal right away. That is fine for a few hundred lines and slow for a few million. For bulk output, write through a buffered StreamWriter:
using var output = new StreamWriter(Console.OpenStandardOutput());
for (int i = 0; i < 1_000_000; i++)
{
output.WriteLine(i);
}
The using declaration disposes the writer at the end of the scope, which flushes the remaining buffer. If you mix this with ordinary Console.WriteLine calls, the two can appear out of order, because they buffer separately.
Console versus Debug output
System.Diagnostics.Debug.WriteLine does not write to the console. It sends text to attached trace listeners, normally the Output window of Visual Studio or VS Code, and the calls are removed from Release builds. Use it for developer diagnostics, and Console (or a logging library) for output users should see.
The classic Main method
Projects created before .NET 6, and many company templates, still use an explicit entry point. It behaves exactly the same:
using System;
namespace CSharpExamples;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
The using System; line is required on .NET Framework and in projects that turn implicit usings off. The file-scoped namespace syntax needs C# 10; on older versions, put the class in a block-scoped namespace CSharpExamples { ... }.