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, always returning it as a string.

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

String Interpolation in Output

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

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

Console.WriteLine($"{name} scored {score}%");
Console.WriteLine($"Next year: {score + 1}");

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}!");

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}");

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.
Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and produce a bool. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting so the second operand is skipped once the result is already determined. For strings, == compares content by default (unlike some languages), since string overrides the equality operator.