C# Output

Almost every program needs a way to show results to the user. In C#, you display text and values on the screen using the Console class. In this lesson you will learn the difference between WriteLine and Write, and how to print numbers and combine text.

Console.WriteLine()

The most common way to print output is Console.WriteLine(). It prints the value you give it and then moves to a new line, so each call appears on its own line.

Printing on separate lines

using System;

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

The program above prints Hello on one line and World on the next line.

Console.Write()

There is also Console.Write(), which prints text but does not move to a new line afterward. This lets you print several pieces of output on the same line.

Printing on the same line

using System;

class Program
{
    static void Main()
    {
        Console.Write("Hello ");
        Console.Write("World");
    }
}
Note: Use WriteLine when you want each output on its own line, and Write when you want output to continue on the same line.

Printing numbers and combining values

You can print numbers directly without quotes, and C# will show the result. You can also join text and values together using the plus sign.

Numbers and combined text

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(25);
        Console.WriteLine("The total is " + 25);
    }
}
MethodBehavior
Console.WriteLine()Prints a value and adds a new line after it
Console.Write()Prints a value without adding a new line
  • Text values must be wrapped in double quotes.
  • Numbers can be printed without quotes.
  • Use the + sign to join text with other values.
  • WriteLine adds a line break, Write does not.

Exercise: C# Output

Which method prints text and then moves to a new line?