Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Console.Beep Method (Int32, Int32)

Plays the sound of a beep of a specified frequency and duration through the console speaker.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static void Beep (
	int frequency,
	int duration
)
public static void Beep (
	int frequency, 
	int duration
)
public static function Beep (
	frequency : int, 
	duration : int
)
Not applicable.

Parameters

frequency

The frequency of the beep, ranging from 37 to 32767 hertz.

duration

The duration of the beep measured in milliseconds.

Exception typeCondition

ArgumentOutOfRangeException

frequency is less than 37 or more than 32767 hertz.

-or-

duration is less than or equal to zero.

HostProtectionException

This method was executed on a server, such as SQL Server, that does not permit access to the console.

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition Platform Note: The frequency and duration parameters are ignored for these operating systems.

This example demonstrates the Beep method by playing the first few notes of a song through the console speaker.

// This example demonstrates the Console.Beep(Int32, Int32) method
using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
// Declare the first few notes of the song, "Mary Had A Little Lamb".
    Note[] Mary = 
        {
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.GbelowC, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.B, Duration.HALF),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.QUARTER),
        new Note(Tone.A, Duration.HALF),
        new Note(Tone.B, Duration.QUARTER),
        new Note(Tone.D, Duration.QUARTER),
        new Note(Tone.D, Duration.HALF)
        };
// Play the song
    Play(Mary);
    }

// Play the notes in a song.
    protected static void Play(Note[] tune)
    {
    foreach (Note n in tune)
        {
        if (n.NoteTone == Tone.REST)
            Thread.Sleep((int)n.NoteDuration);
        else
            Console.Beep((int)n.NoteTone, (int)n.NoteDuration);
        }
    }

// Define the frequencies of notes in an octave, as well as 
// silence (rest).
    protected enum Tone
    {
    REST   = 0,
    GbelowC = 196,
    A      = 220,
    Asharp = 233,
    B      = 247,
    C      = 262,
    Csharp = 277,
    D      = 294,
    Dsharp = 311,
    E      = 330,
    F      = 349,
    Fsharp = 370,
    G      = 392,
    Gsharp = 415, 
    }

// Define the duration of a note in units of milliseconds.
    protected enum Duration
    {
    WHOLE     = 1600,
    HALF      = WHOLE/2,
    QUARTER   = HALF/2,
    EIGHTH    = QUARTER/2,
    SIXTEENTH = EIGHTH/2,
    }

// Define a note as a frequency (tone) and the amount of 
// time (duration) the note plays.
    protected struct Note
    {
    Tone     toneVal;
    Duration durVal;

// Define a constructor to create a specific note.
    public Note(Tone frequency, Duration time)
        {
        toneVal = frequency;
        durVal  = time;
        }

// Define properties to return the note's tone and duration.
    public Tone NoteTone { get{ return toneVal; } }
    public Duration NoteDuration { get{ return durVal; } }
    }
}
/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.
*/

// This example demonstrates the Console.Beep(Int32, Int32) method
import System.*;
import System.Threading.*;

class Sample
{
    public static void main(String[] args)
    {
        // Declare the first few notes of the song, "Mary Had A Little Lamb".
        Note mary[] =  {
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a), new Duration(Duration.quarter)),
            new Note(new Tone(Tone.gbelowC),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.b),new Duration(Duration.half)),
            new Note(new Tone(Tone.a),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.a),new Duration(Duration.half)),
            new Note(new Tone(Tone.b),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.d),new Duration(Duration.quarter)),
            new Note(new Tone(Tone.d),new Duration(Duration.half)) };

        // Play the song
        Play(mary);
    } //main

    // Play the notes in a song.
    protected static void Play(Note tune[])
    {
        for (int iCtr = 0; iCtr < tune.length; iCtr++) {
            Note n = tune[iCtr];
            if (n.get_NoteTone().get_member() == Tone.rest) {
                System.Threading.Thread.Sleep((n.get_NoteDuration().get_member()));
            }
            else {
                Console.Beep(n.get_NoteTone().get_member(),
                    n.get_NoteDuration().get_member());
            }
        }
    } //Play

    // Define the frequencies of notes in an octave, as well as 
    // silence (rest).
    protected static class Tone
    {
        private int member;
        Tone()
        {
            member = 0;
        } //Tone
        Tone(int n)
        {
            member = n;
        } //Tone
        public int get_member()
        {
            return member;
        } //get_member
        public void set_member(int n)
        {
            member = n;
        } //set_member
        public static int rest = 0;
        public static int gbelowC = 196;
        public static int a = 220;
        public static int asharp = 233;
        public static int b = 247;
        public static int c = 262;
        public static int csharp = 277;
        public static int d = 294;
        public static int dsharp = 311;
        public static int e = 330;
        public static int f = 349;
        public static int fsharp = 370;
        public static int g = 392;
        public static int gsharp = 415;
    } //Tone

    // Define the duration of a note in units of milliseconds.
    protected static class Duration
    {
        private int member;
        Duration()
        {
            member = 1600;
        } //Duration
        Duration(int n)
        {
            member = n;
        } //Duration
        public int get_member()
        {
            return member;
        }// get_member
        public void set_member(int n)
        {
            member = n;
        }//set_member
        public static int whole = 1600;
        public static int half = whole / 2;
        public static int quarter = half / 2;
        public static int eighth = quarter / 2;
        public static int sixteenth = eighth / 2;
    } //Duration

    // Define a note as a frequency (tone) and the amount of 
    // time (duration) the note plays.
    protected static class Note
    {
        private Tone toneVal;
        private Duration durVal;

        // Define a constructor to create a specific note.
        public Note(Tone frequency, Duration time)
        {
            toneVal = frequency;
            durVal = time;
        } //Note

        // Define properties to return the note's tone and duration.
        /** @property 
         */
        public Tone get_NoteTone()
        {
            return toneVal;
        } //get_NoteTone
        /** @property 
         */
        public Duration get_NoteDuration()
        {
            return durVal;
        } //get_NoteDuration
    } //Note
} //Sample 
/*
This example produces the following results:

This example plays the first few notes of "Mary Had A Little Lamb" 
through the console speaker.
*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft