Need help with list

This is my code, the problem i am having is that the list only shows the last value entered… im sure it is an easy fix, im new to c#… thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListofNames
{
class Program
{
static void Main(string args)
{
List names = new List();

        string option;
        bool running = true;

        Console.WriteLine("List of Names");
        Console.WriteLine("-------------\n\n");

        while (running)
        {
            
            EnterName:  Console.WriteLine("\nEnter Name:");
            string name = Console.ReadLine();
            names.Add(name);


            Console.WriteLine("\nWould you like to add another name? y/n?");
            option = Console.ReadLine();

            if (option.ToLower() != "y")
            {
                foreach (string entry in names)
                {
                    Console.WriteLine("\nThis is the current list of names");
                    Console.WriteLine(name);

                }

                running = false;
                Console.WriteLine("press any key to exit");
                Console.ReadKey();
            }
            else
            {
                goto EnterName;
            }
        

        }


    }
}

}

1 Like

If you want the output to look similar to this:

This is the current list of names
Bart
Bill
Tom
Harry
press any key to exit

You’ll need to do a couple of things. See the comments I added to your code snippet. Good luck && Happy coding!

1 Like