Team LiB
Previous Section Next Section

Printing to the Screen

Most applications communicate information to users by displaying messages via the application's GUI. However, it is also useful at times to be able to display simple text messages to the command line window from which we're running a program as a "quick and dirty" way of verifying that a program is working properly (you'll learn how to run C# programs from the command line in Chapter 13). Until we discuss how to craft a C# GUI in Chapter 16, this will be our program's primary way of communicating with the "outside world."

To print text messages to the screen, we use the following syntax:

Console.WriteLine(expression to be printed);

The Console.WriteLine method can accept very complex expressions, and does its best to ultimately turn these into a single string value, which then gets displayed on the screen. Here are a few examples:

Console.WriteLine("Hi!");  // Printing a string literal/constant.

string s = "Hi!";
Console.WriteLine(s);       // Printing the value of a string variable.
string s = "foo";
string t = "bar";
Console.WriteLine(s + t);   // Using the string concatenation operator (+) to
                           // print "foobar".

int x = 3;
int y = 4;

Console.WriteLine(x);       // Converts x's int value into a string and
                             // prints the value "3" to the screen.

Console.WriteLine(x + y);    // Computes the sum of x and y, then
                             // prints the value "7" to the screen.

Note in the last line of code that the plus sign (+) is interpreted as the integer addition operator, not as the string concatenation operator, because it separates two variables that are both declared to be of type int. So, the sum of 3 + 4 is computed to be 7, which is then printed. In the next example, however, we get different (and arguably undesired) behavior:

Console.WriteLine("The sum of x plus y is: " + x + y);

The preceding line of code causes the following to be printed:

The sum of x plus y is: 34

Why is this?

We evaluate expressions from left to right, and so since the first of the two plus signs separates a string literal and an int, it is interpreted as a string concatenation operator, and the value of x is thus converted into a string, producing the intermediate string value "The sum of x plus y is: 3".

The second plus sign separates this intermediate string value from an int as well (y), so it, too, is interpreted as a string concatenation operator, and the value of y is thus converted into a string, producing the final string value "The sum of x plus y is: 34", which is what finally gets printed.

To print the correct sum of x and y, we must force the second plus sign to be interpreted as an integer addition operator by enclosing the addition expression in nested parentheses:

Console.WriteLine("The sum of x plus y is: " + (x + y));

The nested parentheses cause the innermost expression to be evaluated first; the second plus sign is now seen by the compiler as separating two int values, and will thus serve as the integer addition operator. Then, the first plus sign is seen as separating a string from an int, and is thus treated as a string concatenation operator, ultimately causing this print statement to display the correct message on the screen:

The sum of x plus y is: 7

When writing code that involves complex expressions, it is a good idea to use parentheses liberally to make our intentions clear to the compiler. Extra parentheses never hurt!

Write vs. WriteLine

When we use Console.WriteLine(), whatever expression is enclosed inside the parentheses will be printed, followed by a line terminator. The following code snippet:

Console.WriteLine("First line.");
Console.WriteLine("Second line.");
Console.WriteLine("Third line.");

produces as output:

First line.
Second line.
Third line.

By contrast, the statement

Console.Write(expression to be printed);

causes whatever expression is enclosed in parentheses to be printed without a line terminator. Using Write in combination with WriteLine allows us to build up a single line of output with a series of Write statements, as shown by the following example:

Console.Write("C");       // Using Write here.
Console.Write("SHA");     // Using Write here.
Console.WriteLine("RP");  // Note use of WriteLine as the last statement.

This code snippet produces the single line of output:


When a single print statement gets too long to fit on a single line, as in this example:

    // Pseudocode.
    another statement;
    Console.WriteLine("Here is an example of a single print statement that
is very long ... SO long that it wraps around and makes the program
listing difficult to read");
    yet another statement;

we can make a program listing more readable by breaking up the contents of such a statement into multiple concatenated strings, and then breaking the statement along plus-sign boundaries:

    another statement;
    Console.WriteLine("Here is an example of how " +
                      "to break up a long print statement " +
                      "with plus signs");
    yet another statement;

Even though the preceding statement is broken across three lines of code, it will be printed as a single line of output:

Here is an example of how to break up a long print statement with plus signs.

Escape Sequences

C# defines a number of escape sequences so that we can represent special characters, such as newline and tab characters, in string expressions. The most commonly used escape sequences are listed here:








Vertical tab




Single quote


Double quote

One or more escape sequences can be included in the expression that is passed to the Write and WriteLine methods. For example, consider the following code snippet:

    Console.WriteLine("Presenting ...");
    Console.WriteLine("\n... for a limited \"time\" only ...\n");
    Console.WriteLine("\tBailey the Wonder Dog!");

When the preceding code is executed, the following output is displayed:

Presenting ...

... for a limited "time" only ...

  Bailey the Wonder Dog!

There is a blank line before and after the second line of output because we've inserted extra \n escape sequences in the second statement; the word "time" is quoted because of our use of the \" escape sequences in that same statement; and, the third line of output has been tabbed over one position to the right by virtue of our use of \t.

Team LiB
Previous Section Next Section