Loading...

C# Formatting

Put left, right, and the sum into one printed line, using slots you can widen. Inside Pine.Main, keep int left = 10;, int right = 2;, and int sum = Add(left, right);. Print them with Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);. Run with dotnet run. The screen should show left=10 right=2 sum=12 on one line. A second demo writes {0,4}, {1,4}, and {2,4}, so each number occupies four columns. Leave dates for the next page. This lesson is how those numbers are written, not a new calculation.
Quality and Wrap-up continues, so Pine, Add, left, and right stay. The naming page printed left=10, then right=2, then 12, each from its own WriteLine built with +. Those three lines proved the names. This page keeps the names and the plus inside Add, and changes only the print. If the open file still calls Go and prints x=10, switch to the Add file before you judge a format. A format pattern does not repair a minus. If the body still subtracts, the sum slot shows 8.
The first argument of this WriteLine is the pattern. Later arguments fill the slots in order. {0} is the first of those values, {1} the second, {2} the third. The digits inside the braces are indexes, not the numbers stored in left or right.

One pattern, three slots

Open the Pine file. The class name stays Pine. Keep using System; and keep Add with its four /// lines, name="left" and name="right". Delete leftover Go methods, leftover x and y label prints, and any ReadLine line. The program must not wait for typing.
Inside Main, store the two inputs, call Add, and print once:
int left = 10;
int right = 2;
int sum = Add(left, right);
Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
The pattern is one string. {0} is replaced by left, {1} by right, and {2} by sum. The words left=, right=, and sum= are ordinary text. They are not variables. Spaces you type between those words stay on the screen.

Pine format line for left right sum

csharp
using System;

public class Pine
{
    /// <summary>Adds the two stored numbers.</summary>
    /// <param name="left">The first number.</param>
    /// <param name="right">The second number.</param>
    /// <returns>The sum of left and right.</returns>
    public static int Add(int left, int right)
    {
        return left + right;
    }

    public static void Main()
    {
        int left = 10;
        int right = 2;
        int sum = Add(left, right);
        Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
    }
}

The line is one string

Run dotnet run. The screen shows left=10 right=2 sum=12. There is one line, not three. Main stores ten and two, Add returns twelve, and the format line drops those three values into the pattern. Ten plus two is still twelve. The braces changed the printing, not the arithmetic.
Read the call from left to right. The pattern comes first. Then left, then right, then sum. That order is the index order: 0, then 1, then 2. If you write {1} where you meant the sum, the screen shows 2 in that spot, because {1} is right. If you write {0} twice, left prints twice. The pattern may reuse an index. It may not skip past the values you passed.
A slot with no matching value stops the run. {3} in this call asks for a fourth value, and you only passed three. The program throws a format error and that line does not appear. Fix the index or pass another argument. Do not "fix" it by changing Add.
If you still see three separate lines, left=10 then right=2 then 12, you are running the naming page's concatenation. Replace those + prints with this one format call and run dotnet run again.

Four columns

The second demo keeps Add, left, right, and sum. Only the pattern changes. A comma and a width inside a slot reserve columns. {0,4} means index 0, printed in a field four characters wide. The number sits at the right of that field. Unused columns become spaces.
Console.WriteLine("left={0,4} right={1,4} sum={2,4}", left, right, sum);
10 is two characters, so two spaces sit before it. 2 is one character, so three spaces sit before it. 12 is two characters, so two spaces sit before it. The labels left=, right=, and sum= are not inside the width. Only the value in the slot is padded.

Pine format line with width 4

csharp
using System;

public class Pine
{
    /// <summary>Adds the two stored numbers.</summary>
    /// <param name="left">The first number.</param>
    /// <param name="right">The second number.</param>
    /// <returns>The sum of left and right.</returns>
    public static int Add(int left, int right)
    {
        return left + right;
    }

    public static void Main()
    {
        int left = 10;
        int right = 2;
        int sum = Add(left, right);
        Console.WriteLine("left={0,4} right={1,4} sum={2,4}", left, right, sum);
    }
}

Run dotnet run again. The screen shows left= 10 right= 2 sum= 12. Count the spaces if the line looks uneven: two between left= and 10, three between right= and 2, two between sum= and 12. The stored numbers are still ten, two, and twelve. The extra spaces are empty columns in the field. They are not zeros, and they are not a new value.
A positive width aligns to the right, which is what this demo uses. A negative width, such as {0,-4}, aligns to the left and puts the spaces after the digits. You do not need that form to finish the page. Keep the comma directly after the index, with no space: {0,4} not {0, 4}.
Spaces you type in the source around + inside Add do not pad the screen. return left + right and return left+right both hand back 12. Padding happens only where the pattern asks for a width. That split is the whole point: source spacing can help a reader of the file, and a width changes the columns of the output.
If the line shows left=10 right=2 sum=12 with no extra spaces, you are still on the first pattern. Open the width demo and run it. If the last number is 8, the body is still a minus. The width will pad 8. It will not add.

Which slot is which

The table is the short map of the two patterns. Plain slots write the value with no extra columns. A width reserves columns and aligns right when the width is positive. The index chooses which argument, counting from zero. The first argument after the pattern is always index 0, even when that value is 10.

📊 Format slots for the Pine sum

Slot What it does
Plain {0} writes the first value with no padding
Width {0,4} writes that value in four columns, aligned right
Index {1} writes the second value, not the digit 1

Dates stay shut

You can print left={0} right={1} sum={2} and see left=10 right=2 sum=12. You can print the same values with width 4 and see spaces before the shorter numbers. Add still returns left + right. Choosing a calendar value, or writing a date pattern such as a year and a month, is the next page, dates. This page stops before that job. A date type does not explain {0}, and a width does not create today's date.
Keep the second file if you want the padded line, or the first file if you want the tight line. Keep left, right, and Add. Keep the /// names matched to those parameters. Keep DateTime out of this run. When dotnet run shows the three numbers inside the pattern you typed, this lesson did its job.
One last pass: one Pine, one format call in Main, indexes that exist, no ReadLine, no Go, and no date call. Run dotnet run once more. If the screen matches the pattern, the slots and the sum agree, and the next topic can put a date on the screen.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top