Start learning free

LESSON 1 OF 347 · WELCOME TO C#

Printing to the Console in C#: Console.WriteLine

Lesson Read the full explanation

TL ; DR:

  • The console is a panel that displays important messages, like errors, for developers.


  • In C#, we use the Console.WriteLine() function to print something to the console. For example, this program:

    Console.WriteLine(13);
    

    prints 13 to the console.



Full lesson:

The console is a panel that displays important messages, like errors, for developers.

Much of the work the computer does with our code is invisible to us by default. If we want to see things appear on our screen, we can print to our console directly.


Console.WriteLine():

In C#, we use the Console.WriteLine() method to print something to the console. For example, this program:

Console.WriteLine(13);

logs 13 to the console.

When you run this program, this is what you'll see in your console:

13


We refer to the data printed to the console as the output of the program.


Newline character:

You might have noticed the extra empty line after 13 in our program's output.

It's important to know that the Console.WriteLine() function also prints an invisible newline character after the value we provide.

This newline character inserts a new line in the console. It's like pressing Enter in your text editor to move the cursor to the next line.

This means that if we use Console.WriteLine() multiple times, the values will be printed on separate lines in the console:

Console.WriteLine(1);
Console.WriteLine(2);
Console.WriteLine(3);

The ouput of this program would be:

1
2
3


Assignment
Follow the Coding Tutorial and let's print some values to the console!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · WELCOME TO C#

    Print in C#, explained

    What the console is and why you print

    The console is a text panel where a program shows messages: results, progress notes and error reports. Most of what your code does is invisible. Variables change, conditions are tested, loops run, and nothing appears on screen unless you ask for it. Printing is how you ask. It is also the quickest way to check what a program is doing while you learn.

    The text a program prints is called its output.

    Console.WriteLine

    In C#, you print with the Console.WriteLine method. Put the value you want to print between the parentheses and end the statement with a semicolon:

    Console.WriteLine(13);
    

    Running it shows:

    13
    

    The pieces of that line each have a job:

    • Console is a class from the System namespace that represents the console.
    • WriteLine is a method of that class, reached with a dot.
    • The parentheses hold the argument, the value to print.
    • The semicolon ends the statement. C# needs it after every statement.

    The spelling matters. C# is case-sensitive, so console.WriteLine, Console.writeline and Console.Writeline are all errors.

    The newline

    WriteLine prints the value and then ends the line, as if you had pressed Enter. The next thing printed starts on a new line. That is why several calls produce several lines:

    Console.WriteLine(1);
    Console.WriteLine(2);
    Console.WriteLine(3);
    
    1
    2
    3
    

    The line break comes after the value, so the output ends at the start of an empty line, but no blank line is printed between values. Calling WriteLine() with nothing in the parentheses prints only the line break, which gives you a blank line.

    Its sibling Console.Write prints the value with no line break, so the next output continues on the same line:

    Console.Write("a");
    Console.Write("b");
    Console.WriteLine("c");
    
    abc
    

    What you can print

    You can pass numbers, text, and truth values. Text goes in double quotes, and the quotes are not printed:

    Console.WriteLine(1998);
    Console.WriteLine("hello");
    Console.WriteLine(true);
    Console.WriteLine(3 + 4);
    Console.WriteLine("3 + 4");
    
    1998
    hello
    True
    7
    3 + 4
    

    Two things to notice. A bool prints with a capital letter, True. And 3 + 4 without quotes is an expression, so C# works out 7 first and prints that, while "3 + 4" in quotes is just text and is printed as written.

    Running a snippet on your own machine

    The lesson runs a snippet like Console.WriteLine(13); for you. In a project of your own, C# also needs a place to start. Modern C# (version 9 and later) allows top-level statements, so a file can begin with the statements themselves:

    using System;
    
    Console.WriteLine("Hello!");
    

    The using System; line lets you write Console instead of System.Console. Newer project templates add it automatically. Older style puts the statements inside static void Main() in a class, and the call works the same way.

    Common mistakes

    • Missing semicolon. Console.WriteLine(13) does not compile.
    • Wrong capitalisation. console and writeline are different names from Console and WriteLine.
    • Missing parentheses. Console.WriteLine 13; is not a method call.
    • Quotes around a number you want to compute. "3 + 4" prints the text, not 7.
    • Expecting a blank line. WriteLine ends a line, it does not add an extra empty one.

    The exercise

    The task is to print the number 1998. It takes one statement:

    1. Write Console.WriteLine with the number in the parentheses.
    2. Finish the line with a semicolon.
    Console.WriteLine(1998);
    

    The console then shows 1998 and moves to the next line, which is what the lesson's test checks. A number needs no quotes, and adding them would print the same characters but as text.

    Key takeaways

    • Printing is how you see what a program does.
    • Console.WriteLine(value); prints the value and then a line break.
    • Console.Write(value); prints without the line break.
    • Text needs double quotes, and the quotes don't appear in the output.
    • C# is case-sensitive, and every statement ends in a semicolon.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free