Given a normal Console in C#, the task is to play Beep sound through the Console.
Approach: This can be achieved with the help of Beep() method of Console Class in System package of C#.
The Beep() method of Console Class is used to play a Beep sound through the Console speaker.
Syntax: public static void Beep ();
Exceptions: This method throws HostProtectionException if this method was executed on a server, such as SQL Server, that does not permit access to a user interface.
Below programs show the use of Console.Beep() method:
Program 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GFG {
class Program {
static void Main(string[] args)
{
Console.Beep();
}
}
}
|
Program 2: Play Beep sound n number of times.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GFG {
class Program {
static void Main(string[] args)
{
int n = 5;
for (int i = 1; i < n; i++)
Console.Beep();
}
}
}
|
Note: Please run the programs on offline Visual Studio to experience the output.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Jan, 2019
Like Article
Save Article