Lesson 4 +10 XP

C# Output

C# Output

To display text in the console, use the Console class.

WriteLine vs Write

Console.WriteLine() prints text followed by a new line:

Console.WriteLine("Hello World!");

Console.Write() prints text without adding a new line:

Console.Write("Hello ");
Console.Write("World!");

That prints Hello World! on one line.

Print multiple lines

Console.WriteLine("Hello World!");
Console.WriteLine("I am learning C#");
Console.WriteLine("It is awesome!");

Each WriteLine call starts a new line of output.

Output numbers and calculations

You can output numbers directly or compute values:

Console.WriteLine(3 + 3);
Console.WriteLine(2 * 5);
Console.WriteLine(10 / 2);

This prints:

6
10
5

Output variables

string name = "John";
Console.WriteLine(name);

int age = 30;
Console.WriteLine(age);

The console shows the value stored in each variable.

TL;DR

  • Console.WriteLine() adds a new line after output.
  • Console.Write() does not.
  • You can print text, numbers, expressions, and variables.