Codectionary / Developer documentation / C#

Console.WriteLine / ReadLine

The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, returning a string or null at the end of input.

Read the syntax

Console.WriteLine(value) writes a value followed by a newline; Console.Write(value) does not add that newline. Both write to a console application’s standard output, not to an HTML page.

Console.ReadLine() reads a line of text and may return null at end of input. Parse converts valid text to a number but can throw on bad input; TryParse is better when the user may enter anything.

Syntax

Console.WriteLine(value);

Examples

Basic Output

Writing text and values to the console.

Console.WriteLine("Hello, World!");
Console.WriteLine(42);

Console.Write("No new line here... ");
Console.Write("still the same line.");
Console.WriteLine();  // move to a new line manually
  1. Writing text and values to the console.
  2. Console.WriteLine(value) writes a value followed by a newline; Console.Write(value) does not add that newline. Both write to a console application’s standard output, not to an HTML page.
  3. The first two calls print Hello, World! and 42 on separate lines. The two Write calls share a line, and the empty WriteLine finishes it without adding text.

String Interpolation in Output

The idiomatic, modern way to build output strings in C#, using the $ prefix.

string name = "John";
int score = 87;

Console.WriteLine($"{name} scored {score}%");
Console.WriteLine($"Next year: {score + 1}");
  1. The idiomatic, modern way to build output strings in C#, using the $ prefix.
  2. Console.WriteLine(value) writes a value followed by a newline; Console.Write(value) does not add that newline. Both write to a console application’s standard output, not to an HTML page.
  3. The $ prefix lets braces evaluate name, score, and score + 1. The output is John scored 87% followed by Next year: 88; the percent sign is literal text.

Reading User Input

Prompting for and reading a line of text from the console.

Console.Write("What is your name? ");
string? name = Console.ReadLine();

Console.WriteLine($"Hello, {name}!");
  1. Prompting for and reading a line of text from the console.
  2. Console.WriteLine(value) writes a value followed by a newline; Console.Write(value) does not add that newline. Both write to a console application’s standard output, not to an HTML page.
  3. The prompt stays on the current line because Write omits a newline. ReadLine waits for console input; if the user types Sam, the greeting becomes Hello, Sam!.

Reading and Converting Numeric Input

Console.ReadLine() always returns a string, so numeric input needs explicit conversion.

Console.Write("Enter your age: ");
string? input = Console.ReadLine();
int age = int.Parse(input ?? "0");

Console.WriteLine($"Next year you will be {age + 1}");
  1. Console.ReadLine() always returns a string, so numeric input needs explicit conversion.
  2. Console.WriteLine(value) writes a value followed by a newline; Console.Write(value) does not add that newline. Both write to a console application’s standard output, not to an HTML page.
  3. ReadLine returns text, and int.Parse turns that text into an int before adding one. Empty input falls back to 0, but nonnumeric input still throws; use TryParse for robust user input.

Best practices

  • Use string interpolation ($"...") instead of string concatenation with + for building readable, dynamic output
  • Remember Console.ReadLine() can return null (e.g. if input is redirected from an empty stream) - handle that case rather than assuming a value
  • Use int.TryParse() instead of int.Parse() when reading user input you have not already validated, to avoid a FormatException crashing the program
  • Reach for a proper logging framework instead of Console.WriteLine() in real applications beyond simple scripts and console tools

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading