Skip to content
Related Articles

Related Articles

Improve Article

C# | How to play Beep sound through Console

  • Last Updated : 29 Jan, 2019

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:




// 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();
    }
}
}

Program 2: Play Beep sound n number of times.




// 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();
    }
}
}

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :