Basic Output: Console.Write* Placeholders

How do I print things out (how do I display information on the screen) in a C# Console app?

Model 1: Console.WriteLine with a single argument

Relevant Program Code

Console.WriteLine("This is printed verbatim (exactly as written here)");
int number;
number = 10;
Console.WriteLine(number);

Program Output

This is printed verbatim (exactly as written here)
10
Press any key to continue . . .

Critical Thinking Questions

  1. Which line of the 'Relevant Program Code' produces the following output?

    This is printed verbatim (exactly as written here) Line 1 - Console.WriteLine("This is printed..
  2. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine(number); 10
  3. In general, if Console.WriteLine is told to print out a single 'thing' (whether that 'thing' is a string (text), a variable,etc), what does Console.WriteLine do?

    It prints that thing to the screen, on it's own line
  4. If the 'Relevant Program Code' was embedded in Main() and then it was run (executed), what output would this section of code produce?
    It will produce the output listed under "Program Output"
  5. If the 'Relevant Program Code' was embedded in another method (other than Main()) and then that method was run (executed), what output would this section of code produce?
    It will produce the output listed under "Program Output". 
    The rest of the program may produce additional output before or after this snippet of code
  6. Briefly think about and then discuss the following question: Why aren't we showing you the rest of the program?
    To help you get used to looking at snippets, knowing that they'll do the same thing, no matter where they are in the program

Model 2: Console.WriteLine with a single placeholder

Relevant Program Code

int number;
number = 10;
Console.WriteLine("Number is: {0}", number);
Console.WriteLine("Number is: {0} again: {0}", number);

Program Output

Number is: 10
Number is: 10 again: 10
Press any key to continue . . .

Critical Thinking Questions

  1. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine("Number is: {0}", number); Number is: 10
  2. In your answer to your prior question, the {0} does not appear in the output. What has it been replaced with? 10, which is the VALUE of the variable 'number' (not the name of the variable - 'number')

  3. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine("Number is: {0} again: {0}", number); Number is: 10 again: 10
  4. In the above code snippet, what does the {0} mean? What does it do / what effect does it have when used in the Console.WriteLine command?It replaces the {0} placeholder with the value of the variable


Model 3: Console.WriteLine with a several placeholders

Relevant Program Code

    int number;
    number = 10;

    // Note the single-line declaration AND initialization
    // for otherNumber:
    int otherNumber = 20;
    // "declaration" = create the variable
    // "initialize" = give the variable it's first value

    Console.WriteLine("Number: {0} otherNumber: {1}", number, otherNumber);

    // swap numbers to print backwards
    Console.WriteLine("Number: {1} otherNumber: {0}", number, otherNumber); 

Program Output

    Number: 10 otherNumber: 20
    Number: 20 otherNumber: 10
    Point is located at: (10, 20, 30)
    Press any key to continue . . .

Critical Thinking Questions

  1. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine("Number: {0} otherNumber: {1}", number, otherNumber); Number: 10 otherNumber: 20
  2. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine("Number: {1} otherNumber: {0}", number, otherNumber);
    Number: 20 otherNumber: 10
  3. In the following line of 'Relevant Program Code', what does the {0} refer to? What does the {1} refer to?

    Console.WriteLine("Number: {1} otherNumber: {0}", number, otherNumber); {0} refers to the value of number; {1} refers to otherNumber
  4. Describe how the number inside the curly braces ( { } ) relates to the things that follow that first parameter to Console.WriteLine() 0 is the first item, 1 is the second, 2 is the third, etc

  5. Predict the output of the following code:

    int x = 10;
    int y = 20;
    int z = 30;
    Console.WriteLine("Point is located at: ({0}, {1}, {2})", x, y, z);
    Console.WriteLine("Point is located at: ({2}, {1}, {0})", x, y, z);
    Point is located at: (10, 20, 30)
    Point is located at: (30, 20, 10)
  6. If you saw code like the following, what do you think would happen?:

    int x = 10;
        Console.WriteLine("x is: {0}  y is: {20}", x );
    "It wouldn't work" - either crash, left blank, etc (C.WL will actually throw an exception)
  7. Let's say that you have the following code:

        double  totalGuesses = 3;
        double numGuessesUsed = 0;

    Furthermore, let's say that you wanted to print out the following message:

    You have a total of 3 guesses, and you have tried to guess the secret number 0 times.

    Write a line of code (that will be placed after the two lines of code listed above) that will print out a message like the above message (but with the current value of totalGuesses and numGuessesUsed.

    Console.WriteLine("You have a total of {0} guesses, and you have tried to guess the secret number {1} times.", totalGuesses, numGuessesUsed);
  8. Building on your answer to the previous problem, write a different line of code that will still output the same message. Hint: Do this by rearranging the variables, and then fixing the rest of the line.

    Console.WriteLine("You have a total of {1} guesses, and you have tried to guess the secret number {0} times.", numGuessesUsed, totalGuesses);

Model 4: Console.Write vs. Console.WriteLine

Relevant Program Code

    Console.WriteLine("A1");
    Console.WriteLine("B1");
    Console.WriteLine("C1");
    Console.WriteLine();
    Console.WriteLine("Separator");
    Console.WriteLine();
    Console.Write("A2");
    Console.Write("B2");
    Console.Write("C2");
    Console.WriteLine();
    Console.WriteLine("End");

Program Output

    A1
    B1
    C1

    Separator

    A2B2C2
    End
    Press any key to continue . . .

Critical Thinking Questions

  1. What line of the 'Relevant Program Code' produces the line of output that reads A1? Which line of code produces B1?Which line of code produces C1?

    Console.WriteLine("A1");, "B1", and "C1"
  2. Briefly summarize what the Console.WriteLine method does, making sure to be clear about which lines it produces the output on

    It FIRST prints the output THEN moves to the next line
  3. Why is there a blank line between the output C1 and Separator?

    Because of the Console.WriteLine();, which just moves down a line
  4. What line of the 'Relevant Program Code' produces the line of output that reads A2? Which line of code produces B2?Which line of code produces C2?

    Console.Write("A2");, "B2", and "C2"
  5. Briefly summarize what the Console.Write method does, making sure to be clear about which lines it produces the output on

    It FIRST prints the output and stays on that same line
  6. What output would the following code produce?

                        Console.Write("A1");
                        Console.WriteLine("B1");
                        Console.Write("C1");
                        Console.WriteLine("D1");
    A1B1 on one line, C1D1 on the next
  7. Write a single line of code that will produce the output Point is at (0, 1, 2), given the following code snippet.
    Next, write several lines of code that will produce the same output .

                        int x = 0, y = 1, z = 2;
    Console.WriteLine("Point is at ({0}, {1}, {2})", x, y, z); // make sure to watch for parens and commas
    Console.Write("Point is at ({0}", x);
    Console.Write(" {1},", y);
    Console.WriteLine(" {2})", z); // must use a WriteLine to generate identical output

Model 5: Output via string concatenation

Relevant Program Code

     int number;
    number = 10;
    Console.WriteLine("Number is:  + number");
    Console.WriteLine("Number is: " + number);
    int otherNumber = 20;
    Console.WriteLine("Number is: " + number + " and: " + otherNumber);

    Console.WriteLine("Number is: " + number + number);
    Console.WriteLine(number + number);
    Console.WriteLine("Number is: " + (number + number));
    int otherNumber = 20; // note single line
    Console.WriteLine("Number: " + number + "otherNumber: " + otherNumber);
    Console.WriteLine("Number: " + otherNumber + "otherNumber: " + number); // POSSIBLE ERROR

    int x = 10;
    int y = 20;
    int z = 30;
    Console.WriteLine($"Point is located at: (" + x + ", " + y + ", " + z + ")");

Program Output

Number is:  + number
Number is: 10
Number is: 10 and: 20
Number is: 1010
20
Number is: 20

Critical Thinking Questions

  1. What line of the 'Relevant Program Code' produces the line of output that reads Number is:  + number?
    Which line of code produces Number is: 10?

    Console.WriteLine("Number is:  + number"); produces "Number is:  + number"
    Console.WriteLine("Number is: " + number); produces "Number is:  10"
  2. How do the two lines (that you identified in the previous question) differ?

    For the second, the + is outside the quotes
  3. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine("Number is: " + number + " and: " + otherNumber);
    Number is: 10 and: 20
  4. In general, what does the '+' symbol do?
    It glues together two pieces of information so that they're all displayed as one string

  5. Look up the word 'concatenation'
    Discuss how that concept is being used in this program snippet.
    The + symbol is concatenting the variable onto the string to produce the output

  6. What line of the 'Relevant Program Code' produces the line of output that reads Number is: 1010?
    Which line of code produces 20?
    Which line of code produces Number is: 20?

    Console.WriteLine("Number is: " + number + number); produces "Number is:  1010"
    Console.WriteLine(number + number); produces "20"
    Console.WriteLine("Number is: " + (number + number)); produces "Number is:  20"
  7. When does + mean string concatenation, and when does it mean mathematical addition?
    + means string concatenation when it's applied to at least one string
    + means mathematical addition when applied to two numbers BEFORE being glued onto a string

  8. Write a single line of code that will produce the output Point is at (0, 1, 2), given the following code snippet.

                        int x = 0, y = 1, z = 2;
    Console.WriteLine("Point is at: (" + x + ", " + y + ", " + z + ")");
  9. Discuss the following in your group, and be prepared to summarize your discussion for the rest of the class.
    What do you like (or dislike) the placeholder syntax?
    What do you like (or dislike) the string concatenation syntax?
    Specifically: Is it distracting to have the "+ var +" splif everywhere, or are they easy to ignore?
    Of the two formats you've seen so far, in which one is it easy to see what will be printed to the screen?

Model 6: Output via string interpolation

Relevant Program Code

    int number;
    number = 10;
    Console.WriteLine("Number is: {number}");
    Console.WriteLine($"Number is: {number}");
    int otherNumber = 20;
    Console.WriteLine($"Number is: {number} and: {otherNumber}");
    Console.WriteLine($"Number is: {number + number}");
    

Program Output

Number is: {number}
Number is: 10
Number is: 10 and: 20
Number is: 20

Critical Thinking Questions

  1. What line of the 'Relevant Program Code' produces the line of output that reads Number is: {number}?
    Which line of code produces Number is: 10?

    Console.WriteLine("Number is: {number}"); produces "Number is: {number}"
    Console.WriteLine($"Number is: {number}"); produces "Number is:  10"
  2. How do the two lines (that you identified in the previous question) differ?

    For the second, there is a $ before the first double-quotes
  3. What output does the following line of 'Relevant Program Code' produce?

    Console.WriteLine($"Number is: {number} and: {otherNumber}");
    Number is: 10 and: 20
  4. What line of the 'Relevant Program Code' produces the line of output that reads Number is: 20?

    Console.WriteLine($"Number is: {number + number}"); produces "Number is:  20"
  5. In the 'string concatenation' approach, a common mistake is to accidentally do string concatenation when one means to do mathematical addition.
    This mistake is much less common in the 'string interpolation' approach.
    Discuss within your group the reason(s) why this is true.

  6. Write a single line of code that will produce the output Point is at (0, 1, 2), given the following code snippet.

                        int x = 0, y = 1, z = 2;
    Console.WriteLine($"Point is located at: ({x}, {y}, {z})");
  7. Discuss the following in your group, and be prepared to summarize your discussion for the rest of the class.
    What do you like (or dislike) the string interpolation syntax?
    Of the three formats you've seen so far, in which one is the easiest to see what will be printed to the screen?

Credits / Copyright Info