Optimizing simple menu in console app I want to optimize code in my simple app that I'm writing to learn C#. I feel it's really badly optimized in terms of good coding practises. Could anyone take a look at it and give me a hint to make it look better?

public void mainMenu(BookList b)
        {
            bool repeat = false;
            Console.WriteLine("Welcome to Library.");
            Console.WriteLine("What do you want to do?");
            Console.WriteLine("1. Add a book.");
            Console.WriteLine("2. Delete book.");
            Console.WriteLine("3. Borrow a book.");
            Console.WriteLine("4. Return book.");
            Console.WriteLine("5. See list of available books.");
            Console.WriteLine("6. See list of all books.");
            Console.WriteLine("7. Exit");
            string userChoice = Console.ReadLine();
            switch  (userChoice)
            {
                case "1":
                    do
                    {
                        b.addBook();
                        Console.WriteLine("What do you want to do now?");
                        Console.WriteLine("Add another book? - Press 1.");
                        Console.WriteLine("Go back to main menu? - Press any other button.");
                        ConsoleKeyInfo keyPressed;
                        keyPressed = Console.ReadKey();
                        if (keyPressed.Key == ConsoleKey.D1 || keyPressed.Key == ConsoleKey.NumPad1)
                        {
                            repeat = true;
                            Console.Clear();
                        }
                        else
                        {
                            repeat = false;
                        }
                    }
                    while (repeat == true);
                    Console.Clear();
                    this.mainMenu(b);
                    break;
                case "2":
                    b.deleteBook();
                    break;
                case "3":
                    b.borrowBook();
                    break;
                case "4":
                    b.returnBook();
                    break;
                case "5":
                    b.displayBooks();
                    break;

            }
        }


I wanted to add repeating an action in addbook() function, but mainMenu is method of different class so I figured it out this way.
Your application is a nice candidate for the command pattern. With a few changes you can make it easily extendable and maintainable. Here's how it goes:

First you need an interface for such a command. Each command needs at least a descritpiton and it must be able to do something:

interface ICommand
{
    string Description { get; }
    void Execute(BookList books);
}


Next we can start implementing the commands:

class AddBookCommand : ICommand
{
    public string Description =&gt; "Add a book.";
    public void Execute(BookList books) { }
}

class DeleteBookCommand : ICommand
{
    public string Description =&gt; "Delete book.";
    public void Execute(BookList books) { }
}

class ExitCommand : ICommand
{
    public string Description =&gt; "Exit.";
    public void Execute(BookList books) { Environment.Exit(0); }
}


Then in your Main you could create all the commands:

var commands = new ICommand[]
{
    new AddBookCommand(),
    new DeleteBookCommand(),
    new ExitCommand()
};


a list of books:

var books = new BookList();


and the main loop:

while (true)
{
    Console.WriteLine("Welcome to Library.");
    Console.WriteLine("What do you want to do?");

    // This loop creates a list of commands:
    for (int i = 0; i &lt; commands.Length; i++)
    {
        Console.WriteLine("{0}. {1}", i + 1, commands[i].Description);
    }

    // Read until the input is valid.
    var userChoice = string.Empty;
    var commandIndex = -1;
    do
    {
        userChoice = Console.ReadLine();
    }
    while (!int.TryParse(userChoice, out commandIndex) || commandIndex &gt; commands.Length);

    // Execute the command.
    commands[commandIndex - 1].Execute(books);
}


This is just a start. You still need to add console maintenance like .Clear  some more error checking etc.