Create a Menu in C# Console Application

This guide is part of a series. For more lessons, visit the C# for Beginners tutorial.

You will make a menu-driven program in this tutorial. Using a while loop, you will learn how to add a menu to a C# console application. This technique helps your users navigate your app, and it enables you to create more robust C# programs.

If you have been following our beginners tutorial series, you have written simple applications that highlight some of the basic functionality of C# and .NET Core. In this tutorial, we will merge some of these examples into a non-trivial application using some of the techniques you have already learned.

Pseudo-code for Console Menu

In short, our menu-driven console application will use a While Loop that will call a Main Menu method. The Main Menu method will print all menu options to the console and wait for the user to choose an option. Once the user selects an option, the program will execute the code block pertaining to the selected option. Here’s how it might look as a pseudo-code:

Main() {
    Loop {
        MainMenu();
    }
}
 
MainMenu() {
    print "option 1";
    print "option 2";
    ...
    wait user input;

    if option1
        do something1;
    if option2
        do something2;
}

The Main() Method

In the previous two tutorials, we learned some useful C# string manipulation techniques – reverse string, and remove whitespace. In this tutorial, our menu-driven console application will use some of the methods we wrote in the previous tutorials and combine them into a useful, fully functional string manipulation program.

We will begin with the static void Main() method. We want the application’s menu to appear until the user chooses to exit the program. Add the following code:

static void Main(string[] args)
{
    bool showMenu = true;
    while (showMenu)
    {
        showMenu = MainMenu();
    }
}

On Line 12, the MainMenu() is invoked inside the while loop. This method will return a bool value, and the loop will iterate as long as that value is true. If the user chooses to exit the program, the MainMenu() method should return a false value. When this happens, the loop will stop and the application will close.

Building the Console Menu

The MainMenu() method should first print the menu options to the console so the user is able to make a selection. Next, it will read the user’s response and determine which option was selected. Based on the option that was selected, the program will call the appropriate method.

private static bool MainMenu()
{
    Console.Clear();
    Console.WriteLine("Choose an option:");
    Console.WriteLine("1) Reverse String");
    Console.WriteLine("2) Remove Whitespace");
    Console.WriteLine("3) Exit");
    Console.Write("\r\nSelect an option: ");
 
    switch (Console.ReadLine())
    {
        case "1":
            ReverseString();
            return true;
        case "2":
            RemoveWhitespace();
            return true;
        case "3":
            return false;
        default:
            return true;
    }
}

On Line 17, I am using Console.Clear() to ensure the menu will be printed onto a blank console screen. This will be important for tidiness, because other methods in my console application will also write to the console window. The menu options themselves are printed on Lines 18-22.

On Lines 24-37, I am using a switch statement. The switch-case construct is a good alternative to using multiple if-else statements. It checks an expression against a number of possible values. In this case, we are reading what the user entered to determine whether they selected option “1,” “2,” or “3.”

Based on the value the user enters, we will invoke either the ReverseString() method or the RemoveWhitespace() method. If the user selects Option 3, the MainMenu() method will return false (Line 33) and the program will terminate. The default case will catch situations where the user enters values other than 1, 2, or 3. In this case, the method will return true and the menu will reset allowing the user to try again.

A Complete Example

Now, we will simply add the ReverseString() and RemoveWhitespace() methods from our previous tutorials and display the results to the user. Here is our complete String Manipulation program:

using System;
 
namespace StringManipulation
{
    class Program
    {
        static void Main(string[] args)
        {
            bool showMenu = true;
            while (showMenu)
            {
                showMenu = MainMenu();
            }
        }
        private static bool MainMenu()
        {
            Console.Clear();
            Console.WriteLine("Choose an option:");
            Console.WriteLine("1) Reverse String");
            Console.WriteLine("2) Remove Whitespace");
            Console.WriteLine("3) Exit");
            Console.Write("\r\nSelect an option: ");
 
            switch (Console.ReadLine())
            {
                case "1":
                    ReverseString();
                    return true;
                case "2":
                    RemoveWhitespace();
                    return true;
                case "3":
                    return false;
                default:
                    return true;
            }
        }
 
        private static string CaptureInput()
        {
            Console.Write("Enter the string you want to modify: ");
            return Console.ReadLine();
        }
 
        private static void ReverseString()
        {
            Console.Clear();
            Console.WriteLine("Reverse String");
 
            char[] charArray = CaptureInput().ToCharArray();
            Array.Reverse(charArray);
            DisplayResult(String.Concat(charArray));
        }
 
        private static void RemoveWhitespace()
        {
            Console.Clear();
            Console.WriteLine("Remove Whitespace");
 
            DisplayResult(CaptureInput().Replace(" ", ""));
        }
 
        private static void DisplayResult(string message)
        {
            Console.WriteLine($"\r\nYour modified string is: {message}");
            Console.Write("\r\nPress Enter to return to Main Menu");
            Console.ReadLine();
        }
    }
}

The Bottom Line

In this tutorial, you learned how to add a menu to your C# console application. You created a method to display a menu and called that method from inside a while loop. You processed the user’s selection and triggered the relevant methods. Finally, you returned a boolean value to determine whether to continue to show the menu. This technique allows you to create more complicated console programs while still maintaining a user-friendly interface. Your end users will be able to easily navigate your application and find the features they need. What kind of programs will you create with this technique? Let me know in the comments!


Was this article helpful? Share it with your friends:

Share on email
Share on facebook
Share on twitter
Share on reddit

5 thoughts on “Create a Menu in C# Console Application”

  1. How would you go about creating multiple menus? Like going from one menu to another and also going back to the previous menu?

    Reply
  2. Hi Zara,

    Below is a working C# Console menu program that adds or subtracts 2 numbers, that is not finished but has 2 menus and can be adapted to have more menus.

    It has A main menu and a Calculations menu and an output subroutine that displays the results. It could have 3 menus at least =: input menu, calculations menu and output menu.

    It is done by re using the above menu code (boolean loops) to call another menu the calculations menu from the main menu, you can go from one menu to another and go back to the previous menu.

    It also uses the keyword ref to pass the address of the variable answer (ans) to the sub routines so that the actual value at that address is changed not merely a copy therefore making the answer available in the output menu. As it is the variable that has been changed not merely a copy of the variable. This basically makes the multi menu not pointless as the correct numbers are available in the other menus.

    Best Regards,

    Mark.

    using System;

    /*
    * Menu Program
    */

    namespace CalculatorTutorial
    {

    class Program2
    {

    public static void AddNum(ref int ans)
    {
    int num1, num2;
    Console.WriteLine(“Enter 1st number”);
    num1 = int.Parse(Console.ReadLine());
    Console.WriteLine(“Enter 2nd number”);
    num2 = int.Parse(Console.ReadLine());

    ans = num1 + num2;
    Console.Write(“2 numbers added is: “);
    Console.WriteLine(ans);
    Console.ReadKey();
    }

    public static void SubNum(ref int ans)
    {
    int num1, num2;
    Console.WriteLine(“Enter 1st number”);
    num1 = int.Parse(Console.ReadLine());
    Console.WriteLine(“Enter 2nd number”);
    num2 = int.Parse(Console.ReadLine());

    ans = num1 – num2;
    Console.Write(“2 numbers subtracted is: “);
    Console.WriteLine(ans);
    Console.ReadKey();
    }

    public static bool CalculationsMenu(ref int ans)
    {

    Console.WriteLine(“Choose an option:”);
    Console.WriteLine(“1) Get and Add 2 numbers”);
    Console.WriteLine(“2) Subtract 2 numbers”);
    Console.WriteLine(“3) Back”);
    switch (Console.ReadLine())
    {
    case “1”:
    AddNum(ref ans);
    return true;
    case “2”:
    SubNum(ref ans);
    return true;
    case “3”:
    Console.WriteLine(“Back”);
    return false;
    default:
    return true;
    }
    }

    public static void Calculations(ref int ans)
    {
    bool showMenu = true;
    while (showMenu)
    {

    showMenu = CalculationsMenu(ref ans);
    }
    }
    public static void Output(int ans)
    {
    Console.Write(“Result is = “);
    Console.WriteLine(ans);
    Console.ReadKey();

    }
    private static bool MainMenu(ref int ans)
    {
    Console.BackgroundColor = ConsoleColor.Blue;
    Console.ForegroundColor = ConsoleColor.White;
    Console.Clear();

    Console.WriteLine(“Choose an option:”);
    Console.WriteLine(“1) Calculations”);
    Console.WriteLine(“2) Output”);
    Console.WriteLine(“3) File”);
    Console.WriteLine(“4) Exit”);
    Console.Write(“\r\nSelect an option: “);

    switch (Console.ReadLine())
    {
    case “1”:
    Calculations(ref ans);
    return true;
    case “2”:
    Output(ans);
    return true;
    case “3”:
    Console.WriteLine(“File”);
    return true;
    case “4”:
    return false;
    default:
    return true;
    }
    }

    static void Main(string[] args)
    {
    bool showMenu = true;
    int ans = 0;
    while (showMenu)
    {

    showMenu = MainMenu(ref ans);
    }
    }

    }
    }

    Reply

Leave a Comment

Looking for more?

You have visited our site before, and we appreciate you!

If you found the tutorials helpful, enter your email address for more free C# tips and tricks.

Can't get enough C#?

Enter your email address for more free C# tutorials and tips.