Console applications, also called terminal window applications, provide the simplest way to read and write input from a development standpoint. You can create a console application in Visual Studio by selecting New Project and then Console Application. Console programs are not ideal for programs with many deployments and users. This section reveals several console methods.
As a quick introduction to the Console class, this program simply prints a series of characters to the terminal window. This will not result in a world-class visual experience, but it is useful for certain type of applications.
--- Program that uses Console type [C#] ---
using System;
class Program
{
static void Main()
{
// Write to console window.
Console.WriteLine("Thanks for visiting Dot Net Perls.");
}
}
--- Result of the program ---
The specified string is written to the terminal.You will want to display information when the console program executes. If the program takes a long time to finish, printing the status of the operation is useful as people won't assume the program froze. These articles talk about writing data to the terminal output.
The Console type has a variety of methods available for you to use to read in user input from the terminal window. When the user types in characters, you can read that instantly with ReadKey or when the Enter key is pressed, using ReadLine.
It is very useful to pass string arguments to your console programs; you can do this by creating a shortcut in Windows to the actual executable. Then, these articles can help you write the C# code to process the arguments.
See Args Loop (Foreach and For).
Occasionally, annoying beeps are useful for programs. This article takes a humorous but functional look at the Beep method on the Console type. Read it at your own peril.
You can also set the title text of a console program. This can help make the title more descriptive; you can even display a progress indicator in the title text if you want.