繁体   English   中英

当我为列表运行 .RemoveAt() 时,C# 程序退出

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

我正在尝试理解 C#。 一切都很顺利,直到我试图从列表中删除一个项目。

当程序到达 mylist.RemoveAt(0) 所在的代码时,它就退出了。 它甚至不显示错误,它只是以代码 (0) 退出。

我想要发生的是删除字符串,并且在“下一次输入.....”和“应该有.....”之间没有任何显示

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();
        }
    }
}

使用 ReadLine() 而不是 Read()。 问题是在第一次 Read 它仍然等待 Enter 继续。 然后它只是滚过第二个 Read() 语句。 逻辑很好,你只是在控制台中看不到结果

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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