C# info re:在StringList中添加/删除方法

C# info re:在StringList中添加/删除方法,c#,C#,当前代码目前正在编译,如果发现重复代码,它将自动删除。如果用户希望在删除之前保留副本,是否可以选择其他方式。或者有没有其他适合我的方法 static void Main(string[] args) { List<string> dictionaryList = new List<string>(); string input; Console.Write("Please enter a string or EN

当前代码目前正在编译,如果发现重复代码,它将自动删除。如果用户希望在删除之前保留副本,是否可以选择其他方式。或者有没有其他适合我的方法

 static void Main(string[] args)
    {
        List<string> dictionaryList = new List<string>();
        string input;

        Console.Write("Please enter a string or END to finish: ");
        input = Console.ReadLine();
        while (input.ToUpper() != "END")
        {
            if (!dictionaryList.Contains(input)) // This is where i am looking of a user response of y/n too add duplicate string

            dictionaryList.Add(input);

            Console.Write("Please enter a string or END to finish: ");
            input = Console.ReadLine();

        }

        dictionaryList.Sort();
        Console.WriteLine("Dictionary Contents:");
        foreach (string wordList in dictionaryList)
            Console.WriteLine("\t" + wordList);
    }
}
}
static void Main(字符串[]args)
{
列表字典列表=新列表();
字符串输入;
编写(“请输入字符串或结束:”);
input=Console.ReadLine();
while(input.ToUpper()!=“END”)
{
if(!dictionaryList.Contains(input))//这是我正在查找的y/n用户响应的地方,请添加重复字符串
dictionaryList.Add(输入);
编写(“请输入字符串或结束:”);
input=Console.ReadLine();
}
dictionaryList.Sort();
Console.WriteLine(“词典内容:”);
foreach(字典列表中的字符串单词列表)
Console.WriteLine(“\t”+单词列表);
}
}
}

这应该可以做到:

while (input.ToUpper() != "END")
{
    bool blnAdd = true;
    if (dictionaryList.Contains(input))
    {
        Console.Write("Already exists, keep the duplicate? (Y/N)");
        blnAdd = Console.ReadLine().Equals("Y");
    }

    if (blnAdd)
        dictionaryList.Add(input);

    Console.Write("Please enter a string or END to finish: ");
    input = Console.ReadLine();
}

代码背后的逻辑:如果列表中已经存在输入,则提醒用户并阅读其答案-仅当添加项目时。

尝试以下代码:

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

            string input;

            Console.Write("Please enter a string or END to finish: ");
            input = Console.ReadLine();
            while (input.ToUpper() != "END")
            {
                if (dictionaryList.Contains(input))
                {
                    Console.Write("Do you want to have dup string(Y/N):");
                    string response = string.Empty;
                    response = Console.ReadLine();
                    if (response.ToUpper().Equals("Y"))
                        dictionaryList.Add(input);    
                }
                else
                {
                    dictionaryList.Add(input);
                }
                Console.Write("Please enter a string or END to finish: ");
                input = Console.ReadLine();

            }

            dictionaryList.Sort();
            Console.WriteLine("Dictionary Contents:");
            foreach (string wordList in dictionaryList)
                Console.WriteLine("\t" + wordList);
List dictionaryList=new List();
字符串输入;
编写(“请输入字符串或结束:”);
input=Console.ReadLine();
while(input.ToUpper()!=“END”)
{
if(字典列表包含(输入))
{
Console.Write(“是否要有dup字符串(Y/N):”;
字符串响应=string.Empty;
response=Console.ReadLine();
if(response.ToUpper().Equals(“Y”))
dictionaryList.Add(输入);
}
其他的
{
dictionaryList.Add(输入);
}
编写(“请输入字符串或结束:”);
input=Console.ReadLine();
}
dictionaryList.Sort();
Console.WriteLine(“词典内容:”);
foreach(字典列表中的字符串单词列表)
Console.WriteLine(“\t”+单词列表);

如果可能有许多重复项,那么字典可能更合适,其中int是字符串/单词出现的次数。我正在测试代码,但如果是Console.Write的响应(“是否希望有dup字符串(Y/N):”;是'n'它将完全删除该字符串,即{输入一二三一(响应'n')四五六结束输出二三四五六}如果用户不愿意保留dup字符串,我想您会删除所有具有输入字符串的项目……对不起,我的错误。只有当用户“错误地”再次输入word时,才会提示用户确认是否希望它显示两次或仅显示第一个条目。完美,因此只需删除删除即可。(atIndex)以确保仅删除1个条目,而不是所有条目。谢谢你。