简体   繁体   English

c#,list.add(Console.ReadLine)时,列表中的打印项目返回空

[英]c#, printing items in list returns empty when list.add(Console.ReadLine)

The console doesn't write the items in the list. 控制台不会在列表中写入项目。 I am trying to add input from user to a list then write to the screen. 我正在尝试将用户的输入添加到列表中,然后写入屏幕。 So once i add input to the list i would want it to print to the screen. 因此,一旦我将输入添加到列表中,我就会希望将其打印到屏幕上。

ArrayList list = new ArrayList();

 switch (userInput)
        {

            case "Add":
                {
                    Console.WriteLine("Enter Fullname: ");
                    Console.ReadLine();
                    list.Add(Console.ReadLine());

                    display();
                    break;
                }


            case "List":
                {

                    foreach (string item in list)
                    {

                        Console.WriteLine(item);
                    }
                    display();
                    break;
                }

        }

You need to assign to a variable and then add 您需要分配一个变量,然后添加

string line = Console.ReadLine();
list.Add(line);

When you write: 当你写:

ArrayList list = new ArrayList();

you delete all the elements in the ArrayList. 您删除ArrayList中的所有元素。

You didn't post how do you get to the "List" section after insertion the elements... 插入元素后,您没有发布如何进入“列表”部分。

also you sholud delete the first 你也应该删除第一个

Console.ReadLine();

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

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