The console is the command line, where you type commands to run code and get a response. In general terms, input and output (I/O) describe the interactions of information being given to the program (input) and information that the program emits.
These interactions are often communication with a user, but can also be communication with other programs. We won’t get that elaborate here, but will describe some interesting I/O capabilities that you can use in a C# application to communicate with people.
1 — There are different ways to write
Console class has two writing methods:
WriteLine. The difference is that
WriteLine adds a new line after printing and
Write doesn’t. Here’s an example:
Console.WriteLine("Console I/O Demo\n");
Console.WriteLine("\t 1. Door #1");
Console.WriteLine("\t 2. Door #2");
Console.WriteLine("\t Q. Quit");
Console.Write("\nYour Choice (1, 2, or Q): ");
Console.WriteLine in the previous post on Running Apps. However, there are a few differences here, specifically in the strings with escape,
\, characters, which are special instructions to the terminal. The first line contains a newline escape,
\n, which adds a line in the terminal where it appears. This example has the effect of adding two newlines because one is added by the call to
WriteLine by design.
The 2nd through 4th lines have a tab,
\t, prefix. This helps to indent each menu options, adding visual cues to the output.
The last line is different because it uses
Console.Write, which doesn’t add a newline to the end of the string. Notice that this is a prompt, where the user would type a letter from the menu and, in this design, we want what they type to appear on the same line.
Another interesting feature is the use of the newline escape,
\n, prefix in the string for
Console.Write. We could have added the newline to the last menu item. However, I thought it was interesting to show another example of how to use escape characters to help format string output. We’ll see more formatting examples both in this post and throughout the series, including when we discuss strings in more depth.