C# Beep Example

Console screenshot

Console.Beep makes noises. You want to make your C# program create a beeping sound, such as for an alarm or to annoy your coworkers while you are away. The Console.Beep method provides an exquisite beeping sound, worthy of several music industry awards for poor sound quality.

This C# example program uses the Console.Beep method to make noises.

Example

Note

First, you must be using a C# console application for this to work. The Console.Beep method has two overloads: the first version is the default beep, and the second version receives two arguments—the frequency and the duration in milliseconds. In this program, we loop through all the frequencies at an increment of 200. This produces a musical effect over a few seconds when executed.

Program that uses Console.Beep method [C#]

using System;

class Program
{
    static void Main()
    {
	// The official music of Dot Net Perls.
	for (int i = 37; i <= 32767; i += 200)
	{
	    Console.Beep(i, 100);
	}
    }
}

Output
    Please run the program on your computer to find out.

Uses

Programming tip

The Console.Beep method can be used for simple alarm programs. For example, you might want to create a program that executes for 30 minutes and then starts beeping. You could even create primitive alarm clocks in C# and use your computer to wake up in the morning.

Alarm Console Program

Note: The Console.Beep method is not supported on some versions of 64-bit Windows operating systems. Thanks to Max for writing in with this information.

Summary

At this point, we have established the musical genius of Dot Net Perls: the amazing beeping effect was presented. Instead of downloading MP3s on your computer, you can simply use a random number generator and the Console.Beep method to generate your own music. A strong painkiller will be required after a few minutes.

Random Number Console Programs
.NET