Now, you know how to read input data, but how can you ask questions to the user or present results on the screen? The answer, together with examples, is shown in this section.
Similarly as in the case of reading data, operations related to the standard output stream are performed using methods of the Console static class from the System namespace, namely Write and WriteLine. Let's see them in action!
To write some text, you can just call the Write method, passing the text as a parameter. An example of code is as follows:
Console.Write("Enter a name: ");
The preceding line causes the following output to be shown:
Enter a name:
What's important here is that the written text is not followed by the line terminator. If you want to write some text and move to the next line, you can use the WriteLine method, as shown in the following code snippet:
Console.WriteLine("Hello!");
After executing this line of code, the following output is presented:
Hello!
Of course, you can also use Write and WriteLine methods in more complex scenarios. For example, you can pass many parameters to the WriteLine method, namely the format and additional arguments, as shown in the following part of the code:
string name = "Marcin";
Console.WriteLine("Hello, {0}!", name);
In this case, the line will contain Hello, a comma, a space, a value of the name variable (that is, Marcin), as well as the exclamation mark. The output is shown as follows:
Hello, Marcin!
The next example presents a significantly more complex scenario of writing the line regarding the confirmation of a table reservation at a restaurant. The output should have the format Table [number] has been booked for [count] people on [date] at [time]. You can achieve this goal by using the WriteLine method, as shown as follows:
string tableNumber = "A100";
int peopleCount = 4;
DateTime reservationDateTime = new DateTime(
2017, 10, 28, 11, 0, 0);
CultureInfo cultureInfo = new CultureInfo("en-US");
Console.WriteLine(
"Table {0} has been booked for {1} people on {2} at {3}",
tableNumber,
peopleCount,
reservationDateTime.ToString("M/d/yyyy", cultureInfo),
reservationDateTime.ToString("HH:mm", cultureInfo));
The example starts with a declaration of four variables, namely tableNumber (A100), peopleCount (4), and reservationDateTime (10/28/2017 at 11:00 AM), as well as cultureInfo (en-US). Then, the WriteLine method is called passing five parameters, namely the format string followed by arguments that should be shown in the places marked with {0}, {1}, {2}, and {3}. It is worth mentioning the last two lines, where the string presenting date (or time) is created, based on the current value of the reservationDateTime variable.
After executing this code, the following line is shown in the output:
Table A100 has been booked for 4 people on 10/28/2017 at 11:00
Of course, in real-world scenarios, you will use read- and write-related methods in the same code. For example, you can ask a user to provide a value (using the Write method) and then read the text entered (using the ReadLine method).
This simple example, which is also useful in the next section of this chapter, is shown as follows. It allows the user to enter data relating to the table reservation, namely the table number and the number of people, as well as the reservation date. When all of the data is entered, the confirmation is presented. Of course, the user will see information about the data that should be provided:
using System;
using System.Globalization;
namespace GettingStarted
{
class Program
{
static void Main(string[] args)
{
CultureInfo cultureInfo = new CultureInfo("en-US");
Console.Write("The table number: ");
string table = Console.ReadLine();
Console.Write("The number of people: ");
string countString = Console.ReadLine();
int.TryParse(countString, out int count);
Console.Write("The reservation date (MM/dd/yyyy): ");
string dateTimeString = Console.ReadLine();
if (!DateTime.TryParseExact(
dateTimeString,
"M/d/yyyy HH:mm",
cultureInfo,
DateTimeStyles.None,
out DateTime dateTime))
{
dateTime = DateTime.Now;
}
Console.WriteLine(
"Table {0} has been booked for {1} people on {2}
at {3}",
table,
count,
dateTime.ToString("M/d/yyyy", cultureInfo),
dateTime.ToString("HH:mm", cultureInfo));
}
}
}
The preceding code snippet is based on the parts of code shown and described previously. After launching the program and entering the necessary data, the output could look as follows:
The table number: A100
The number of people: 4
The reservation date (MM/dd/yyyy): 10/28/2017 11:00
Table A100 has been booked for 4 people on 10/28/2017 at 11:00
Press any key to continue . . .
When the code is created, it is a good idea to improve its quality. One of the interesting possibilities associated with the IDE is related to removing unused using statements, together with sorting the remaining ones. You can easily perform such an operation by choosing the Remove and Sort Usings option from the context menu in the text editor.