简体   繁体   中英

C# program exits when I run .RemoveAt() for Lists

I'm trying to understand C#. Every thing was going fine until I tried to remove an item from a List.

when the program gets to the code where mylist.RemoveAt(0) is, it just quits. it doesn't even show an error, it just quits with code (0).

What I want to happen is for the string to get deleted and nothing show up between "next enter....." and "there should be....."

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

namespace understanding_c_sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> mylist = new List<string>();

            mylist.Add("this is my first addition to the list");
            for(int i = 0; i < mylist.Count; i++)
            {
                Console.WriteLine(mylist[i]);
            }
            Console.WriteLine("next enter will delete the first entry");
            Console.Read();
            mylist.RemoveAt(0);

            for (int i = 0; i < mylist.Count; i++)
            {
                Console.WriteLine(mylist[i]);
            }
            Console.WriteLine("there should not have been any entry between this and the last time");
            Console.Read();
        }
    }
}

Use ReadLine() not Read(). The problem is at the first Read it still waits for Enter to continue. Then it just rolls past the second Read() statement. The logic is fine, you just don't see the result in the Console

List<string> mylist = new List<string>();

mylist.Add("this is my first addition to the list");
for(int i = 0; i < mylist.Count; i++)
{
    Console.WriteLine(mylist[i]);
}
Console.WriteLine("next enter will delete the first entry");
Console.ReadLine();
mylist.RemoveAt(0);

for (int i = 0; i < mylist.Count; i++)
{
    Console.WriteLine(mylist[i]);
}
Console.WriteLine("there should not have been any entry between this and the last time");
Console.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM