title: How to improve my menu (console) in c #
body: <p>I created a menu with sub menus in console. Everything works fine but I wonder if it is not possible to improve the structure of my program. To simplify it and make it more generic.</p>
  
  <pre><code>class MainClass
  {
      public static void Main(string[] args)
      {
          MainMenu();
      }
  
      public static void MainMenu()
      {
          Console.WriteLine("Main Menu");
          Console.WriteLine("--------------------");
          Console.WriteLine("[1] Show activities");
          Console.WriteLine("[2] Show teachers");
          Console.WriteLine("[3] Show students");
          Console.WriteLine("[4] Exit the program");
          Console.WriteLine("--------------------\n");
          Console.WriteLine("Please select an option from 1-4\n");
  
          string choice = Console.ReadLine();
          int number;
          bool result = Int32.TryParse(choice, out number);
          if (result)
          {
              Console.Clear();
              SubMenu(number);
          }
          else
          {
              Console.WriteLine("Incorrect choice");
          }
      }
  
      public static void SubMenu(int mainMenuChoice)
      {
          switch (mainMenuChoice)
          {
              case 1:
                  Console.WriteLine("Activities");
                  Console.WriteLine("[1] Show all activities");
                  Console.WriteLine("[2] Find a activity by his code");
                  Console.WriteLine("[3] Return Main Menu");
                  Console.WriteLine("[4] Exit the program");
                  Console.WriteLine("--------------------\n");
                  Console.WriteLine("Please select an option from 1-4\n");
                  break;
  
              case 2:
                  Console.WriteLine("Teachers");
                  Console.WriteLine("[1] Show all teachers");
                  Console.WriteLine("[2] Find a teacher by his matricule");
                  Console.WriteLine("[3] Return Main Menu");
                  Console.WriteLine("[4] Exit the program");
                  Console.WriteLine("--------------------\n");
                  Console.WriteLine("Please select an option from 1-4\n");
                  break;
  
              case 3:
                  Console.WriteLine("Students");
                  Console.WriteLine("[1] Show all students");
                  Console.WriteLine("[2] Find a student by his matricule");
                  Console.WriteLine("[3] Return Main Menu");
                  Console.WriteLine("[4] Exit the program");
                  Console.WriteLine("--------------------\n");
                  Console.WriteLine("Please select an option from 1-4\n");
                  break;
  
              case 4:
                  Environment.Exit(0);
                  break;
          }
          string choice = Console.ReadLine();
          int number;
          bool result = Int32.TryParse(choice, out number);
          if (result)
          {
              Action(mainMenuChoice, number);
          }
          else
          {
              Console.WriteLine("Incorrect choice");
          }
      }
      public static void Action(int menu, int choice)
      {
          switch (menu)
          {
              case 1:
                  switch (choice)
                  {
                      case 1:
                          // Do Stuff
                          break;
  
                      case 2:
                          // Do Stuff
                          break;
  
                      case 3:
                          Console.Clear();
                          MainMenu();
                          break;
  
                      case 4:
                          Environment.Exit(0);
                          break;
                  }
                  break;
  
              case 2:
                  switch (choice)
                  {
                      case 1:
                          // Do Stuff
                          break;
  
                      case 2:
                          // Do Stuff
                          break;
  
                      case 3:
                          Console.Clear();
                          MainMenu();
                          break;
  
                      case 4:
                          Environment.Exit(0);
                          break;
                  }
                  break;
  
              case 3:
                  switch (choice)
                  {
                      case 1:
                          // Do Stuff
                          break;
  
                      case 2:
                          // Do Stuff
                          break;
  
                      case 3:
                          Console.Clear();
                          MainMenu();
                          break;
  
                      case 4:
                          Environment.Exit(0);
                          break;
                  }
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Currently if I have to add a sub menu, I have to add a line to the MainMenu() function, I must add a case in the SubMenu() function and as many as there are choices for this menu in Action().
  For only one sub menu it's ok but if I have to add a dozen it quickly becomes unmanageable.
  I should probably go through one or more classes but I'm lost on the structure.</p>