You saw in the previous example how you can use the Console::Write() and Console::WriteLine() methods to write a string or other items of data to the command line. You can put a variable of any of the types you have seen between the parentheses following the function name and the value will be written to the command line. For example, you could write the following statements to output information about a number of packages:
Executing these statements will produce the output:
There are 25 packages.
The output is all on the same line because the first two output statements use the Write() function, which does not output a newline character after writing the data. The last statement uses the WriteLine() function, which does write a newline after the output so any subsequent output will be on the next line.
It looks a bit of a laborious process having to use three statements to write one line of output, and it will be no surprise that there is a better way. That capability is bound up with formatting the output to the command line in a .NET Framework program, so you’ll explore that next.
int packageCount = 25; // Number of packages
Console::Write(L”There are “); // Write string - no newline
Console::Write(packageCount); // Write value -no newline
Console::WriteLine(L” packages.”); // Write string followed by newline
Console::Write(L”There are “); // Write string - no newline
Console::Write(packageCount); // Write value -no newline
Console::WriteLine(L” packages.”); // Write string followed by newline
There are 25 packages.
The output is all on the same line because the first two output statements use the Write() function, which does not output a newline character after writing the data. The last statement uses the WriteLine() function, which does write a newline after the output so any subsequent output will be on the next line.
It looks a bit of a laborious process having to use three statements to write one line of output, and it will be no surprise that there is a better way. That capability is bound up with formatting the output to the command line in a .NET Framework program, so you’ll explore that next.