Allowing an user to remove an item from a List


I'm self-teaching myself C# and have been stuck trying to figure out on how to allow a user to be able to remove an item along with its index number from a list by either entering their index number or typing the word in.

I've already googled and tried out many ways to do it, but each time I figured out a way, it would delete the element I would choose, but the index didn't go away. Example( List : 0.hat, 1.mat, 2.fat and whenever I would enter "1" or "mat" to remove 'mat' it would then display the list as 0.hat 1.fat and I want it to display 0.hat, 2.fat)

This is what I recently tried doing:

  string[] stringList = new string[] { "hat", "mat", "fat" };
        //Creating list
        List<string> list = new List<string>(stringList);
        string answer;
        //ordering list backwards Z-A
        list.Sort((a, b) => -1 * a.CompareTo(b));
        //loop to allow them to continue removing items
        while (true)
            //Displaying list to console
            for (int i = 0; i < list.Count; i++)
                //display list
                Console.WriteLine("{0}.{1}", i, list[i]);

            //This is blank
            //instructions what to do
            Console.WriteLine("Choose from the list to remove an item: ");
            //this will store the users input
            answer = Console.ReadLine();
            answer = answer.ToLower();

            -- this is where I put the removing at --

            //Making sure user does not crash program
            if (!string.IsNullOrEmpty(answer))
                var index = list.FindIndex(i => i == answer);
                foreach (var item in list)
                    if (index >= 0)

The method I used here doesn't remove anything. I'm having a hard time understanding. If someone could provide some insight that would be great. Thanks

Show source
| generics   | list   | c#   2017-01-05 18:01 4 Answers

Answers ( 4 )

  1. 2017-01-05 18:01

    You can remove the string without having to look for the index by passing the string into .Remove() method. Also remove the foreach loop as it is redundant, you are not doing anything there.

    if (!string.IsNullOrEmpty(answer))

    Using Dictionary you can access key or value and remove as you need.

    var list = new Dictionary<int, string>
        { 0, "hat" },
        { 1, "mat" },
        { 2, "fat" }
    var item = list.FirstOrDefault(kvp => kvp.Value == "hat");
    // Remove by value
    // Remove by key

    Print the results

    foreach (var kvp in list)
        Console.WriteLine(kvp.Key + " " + kvp.Value);
  2. 2017-01-05 18:01

    Instead of using list, use dictionary, where key would be an index of item. In this case, you'll preserve original indices of items, when you remove one.

  3. 2017-01-05 19:01

    Remove by data


    Remove by index

    var index = list.FindIndex(i => i == answer);
  4. 2017-01-05 19:01

    Original question:

    how to "remove an item along with its index number from a list by either entering their index number or typing the word in."

    There are a lot of ways to do this in C#. There are even multiple types. You've discovered one, List<T>, and that's certainly one way to do it, but probably not the best. You could also do an array of strings. There are several other types of collections available in the Systems.Collections.Generic namespace, along with List<T>.

    But easiest by far is to just use Dict<TKey, TValue>.

    How? Look at examples in the link I provided, or do something like this:

    var a = new Dictionary<int, string>(){
        {0, "hat"},
        {1, "mat"},
        {2, "fat"}
    a.Remove(0); // remove by key
    a.Where(i => i.Value != "mat"); // remove by value
◀ Go back