Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to play Beep sound through Console in C#?
The Console.Beep() method in C# is used to play a beep sound through the system speaker. This method is particularly useful for providing audio feedback in console applications or alerting users to specific events.
Syntax
The Console.Beep() method has two overloads −
Console.Beep();
Console.Beep(int frequency, int duration);
Parameters
frequency − The frequency of the beep in hertz (Hz). Must be between 37 and 32767.
duration − The duration of the beep in milliseconds.
Using Console.Beep() Without Parameters
The parameterless Console.Beep() method plays a default beep sound −
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("Playing default beep sound...");
Console.Beep();
Console.WriteLine("Beep sound played successfully!");
}
}
The output of the above code is −
Playing default beep sound... Beep sound played successfully!
Using Console.Beep() with Frequency and Duration
You can specify custom frequency and duration to create different beep sounds −
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("Playing custom beep sounds...");
Console.WriteLine("Low frequency beep (200 Hz, 1000ms)");
Console.Beep(200, 1000);
Console.WriteLine("High frequency beep (2000 Hz, 500ms)");
Console.Beep(2000, 500);
Console.WriteLine("Medium frequency beep (800 Hz, 300ms)");
Console.Beep(800, 300);
Console.WriteLine("All beeps completed!");
}
}
The output of the above code is −
Playing custom beep sounds... Low frequency beep (200 Hz, 1000ms) High frequency beep (2000 Hz, 500ms) Medium frequency beep (800 Hz, 300ms) All beeps completed!
Creating Musical Beeps
You can create simple melodies by using different frequencies that correspond to musical notes −
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("Playing a simple melody...");
// C note (261.63 Hz)
Console.Beep(262, 400);
// D note (293.66 Hz)
Console.Beep(294, 400);
// E note (329.63 Hz)
Console.Beep(330, 400);
// F note (349.23 Hz)
Console.Beep(349, 400);
Console.WriteLine("Melody completed!");
}
}
The output of the above code is −
Playing a simple melody... Melody completed!
Common Use Cases
Error Notifications − Alert users when an error occurs
Task Completion − Signal when a long-running process finishes
User Input Validation − Provide audio feedback for invalid input
System Alerts − Create attention-grabbing sounds in console applications
Important Notes
The beep sound plays through the system speaker, not through headphones or external speakers in some systems.
The method may not work on all platforms or in all environments (like some virtual machines).
Frequency values outside the range of 37-32767 Hz will throw an
ArgumentOutOfRangeException.Duration must be a positive value, otherwise an exception will be thrown.
Conclusion
The Console.Beep() method provides a simple way to generate audio feedback in console applications. You can use the default beep or specify custom frequency and duration values to create different sounds for various scenarios in your C# programs.
