C# | How to play Beep sound through Console

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Console.Beep Method
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)
    {
        // Play beep sound once
        Console.Beep();
    }
}
}

chevron_right


Program 2: Play Beep sound n number of times.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Console.Beep Method
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;
  
        // Play beep sound n times
        for (int i = 1; i < n; i++)
            Console.Beep();
    }
}
}

chevron_right


Note: Please run the programs on offline Visual Studio to experience the output.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.