C# Console Programs

Dot Net Perls

.NET Console

Console screenshot

Console programs 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. We see Console methods as well as complete example console programs.

Programs are arguably the most complicated engineering artifacts ever produced; they consist of many many details, every one of which must be correct before the program will work completely. Aho et al., p. 23

Console

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
    The specified string is written to the terminal.

Write. 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. We talk about writing data to the terminal output.

Console.Write Example Console.WriteLine Use Console.SetOut, File OutputColor type

Colors. You can change the foreground color and background color of text you write to the console. This can significantly improve the usability of your console programs. They still won't look great, but they will look better.

Console Color, Text and BackgroundColor

Read. 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.

Console.Read Method Console.ReadKey Method Console.ReadLine Tips

Lock keys. You can also detect whether the caps lock key is pressed on the Console with the CapsLock bool property. The NumberLock property is similar.

Console.CapsLock Property Console.NumberLock Property

Arguments. 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. We write the C# code to process the arguments.

Main Args Examples Args Loop (Foreach and For) Environment GetCommandLineArgsConsole.Title usage

Title. 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.

Console.Title Example

Height and width. You can control the height of your console window with the Console.WindowHeight property. The Console.LargestWindowHeight property can help you determine the optimum height for your window.

Console.WindowHeight

Beep. Occasionally, annoying beeps are useful for programs. We take a humorous but functional look at the Beep method on the Console type. Read it at your own peril.

Beep Example

Programs

Program icon (copyright Microsoft)

Let's explore some complete or nearly-complete program implementations. These programs are useful for testing purposes or for code generation in the C# language. Very long or complex programs are not available here. No downloads are available in binary form.

Note: Dot Net Perls contains thousands of complete programs, but most of them are not useful tools for anything except demonstration.

Benchmark. One of the most useful program implementations is a benchmarking harness. This program will test two pieces of C# code, and then print results of the time required for each piece to execute. It has been used on many benchmarks on this site.

Benchmark ProgramsNote (please read)

Web pages. These programs download specified web pages, which is useful for when you are testing a web site. The code in these programs was used extensively in the development of this website.

Decompress Web Page Download Web Page Time Web Page

Sound alarms. This is a simple program that can be used to sound an alarm using the Console.Beep method. It also uses the Thread.Sleep method to delay that beep a certain number of seconds or minutes.

Alarm Console ProgramThe C# programming language

Generate code. Here we implement a simple code generator. By using this generator, you can replace StartsWith method calls with a much faster alternate implementation.

StartsWith Code Generator

Play sounds. You can play sounds in your C# program using the SoundPlayer type. This program demonstrates how you can use this type, and it requires you to specify a WAV file to play.

Play Sounds With SoundPlayer (WAV)

Summary

Console programs are ideal for situations where the user interface is not important. They provide a way to output important information, but do not over-complicate the software with graphical user interface code.