C# ยท Chapter 5 of 46

C# Output

Console.WriteLine() is used to print output to the screen, followed by a new line. Console.Write() does the same but without adding a new line at the end.

Both methods can print strings, numbers, and other values, and you can combine text and variables using string concatenation or string interpolation.

Syntax
Console.WriteLine(value);
Console.Write(value);

WriteLine vs Write

Console.WriteLine() adds a newline after the text, so the next output appears on a new line. Console.Write() keeps the cursor on the same line, useful for building output piece by piece.

Printing multiple values

You can combine text and variables with the + operator, or use string interpolation with a $ prefix for cleaner code, covered in a later topic.

Example 1 (csharp)
using System;

class Program {
  static void Main() {
    Console.WriteLine("Hello");
    Console.WriteLine("World");
  }
}
Output
Hello
World

Each WriteLine call prints text followed by a new line.

Example 2 (csharp)
using System;

class Program {
  static void Main() {
    Console.Write("Hello ");
    Console.Write("World");
  }
}
Output
Hello World

Write() does not add a new line, so both texts appear on the same line.

Key points

  • Console.WriteLine() prints text and moves to a new line.
  • Console.Write() prints text without a new line.
  • You can print strings, numbers, and other data types.
  • The + operator can join strings and variables together.
๐Ÿ’ก Note: Console.WriteLine() is the most commonly used way to see output while learning and debugging.

๐Ÿ“ Quick Quiz

1. Which method adds a new line after printing?

2. What does Console.Write() do differently from WriteLine()?

3. Which operator can join text and variables?