Hello, I understand that the question is elementary for the majority - I am a newcomer, I will be grateful for reasonable answers. There is this code, how more correctly ( from the side of optimizing memory consumption ) will make a return to the menu of the selection of available program functions, which is in the void SwitchMessage () method, after execution.

As I understand it, there are 3 options:

  1. Recursion inside SwitchMessage ();

  2. goto;

  3. The way that is now implemented in the code.

.

namespace ConsoleApp5 { class Program { Book book = new Book(); void Main() { Programin(1); } void WellcomeMessage () { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Личная Библиотека"); } void SwitchMessage () { int inputSwitchDone; Block1: Console.WriteLine("Для просмотра существующих Книг - введите 1"); Console.WriteLine("Для добавления новой книги - введите 2"); string inputSwitch = Console.ReadLine(); if (int.TryParse(inputSwitch, out inputSwitchDone)) { //SwitchMessage (); //goto Block1; book.Switcher(inputSwitchDone); Programin(2); } else { ErrorMessage(); } Console.ReadKey(); } void Programin(int switch1) { switch (switch1) { case 1: WellcomeMessage(); SwitchMessage(); break; case 2: SwitchMessage(); break; default: break; } } void ErrorMessage () { Console.WriteLine("Введен Неверный тип данных, повторите ввод"); SwitchMessage(); } } } 

    1 answer 1

    For me, so you have written too much code. Look at my example. There, neither recursion nor goto are needed - just a user input cycle:

     class Program { public void Main() { WellcomeMessage(); while (SwitchMessage()) ; } void WellcomeMessage() { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Личная Библиотека"); } bool SwitchMessage() { int inputSwitchDone; Console.WriteLine(); Console.WriteLine("Для просмотра существующих Книг - введите 1"); Console.WriteLine("Для добавления новой книги - введите 2"); Console.WriteLine("Для выхода - введите 3"); string inputSwitch = Console.ReadLine(); if (int.TryParse(inputSwitch, out inputSwitchDone)) { switch (inputSwitchDone) { case 1: Console.WriteLine("Просмотр книг"); break; case 2: Console.WriteLine("Добавление книг"); break; case 3: Console.WriteLine("Выход"); return false; default: ErrorMessage(); break; } } else { ErrorMessage(); } return true; } void ErrorMessage() { Console.WriteLine("Введен Неверный тип данных, повторите ввод"); } } 
    • An interesting approach with while in Main (), and bool SwitchMessage (). Thanks for the answer ! - Andrew Murena