C# Console Overview

Console

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.

Console introduction

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.

Writing to 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.

See Console.Write Example.

See Console.WriteLine Use.

Reading input from terminal

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.

See Console.Read Method.

See Console.ReadKey Method.

See Console.ReadLine Tips.

Arguments to programs

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 Main Args Examples.

See Args Loop (Foreach and For).

Beeping

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.

See Beep Example.

Setting titles of windows

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.

See Console.Title Example.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen