关于c#:如何复制列表框项目以列出列表并将列表与另一个列表进行比较


How to copy listbox items to list and compare the list with another

本问题已经有最佳答案,请猛点这里访问。

我有List<string> myListList<string> myList1对象:我想要
比较它们,如果它们相似,我想弹出一个消息框,但是当我单击按钮时,它什么也没显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
list<string> myList =new list<string>();
list<string> myList1 =new list<string>();          
myList1.Add("a");
myList1.Add("r");
myList1.Add("u");
myList1.Add("y");

foreach (string str in listBox1.Items)
{
    myList.Add(str);
}

if (myList==myList1)
{
    MessageBox.Show("Matched");
}
else { MessageBox.Show("Not matched"); }


List \\是引用,您正在尝试比较2个列表,它们是不同的引用。如果要查看相同项目的顺序是否相同:

1
2
3
4
if(myList.SequenceEquals(myList1))
{
     ....
}

或者它们的顺序可能不同:

1
2
3
4
if(myList.All(myList1.Contains))
{
     ....
}