如何比較兩個列表以確定是否相等(C#)?
設定兩個列表 −
列表一
List < string > list1 = new List < string > ();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");列表二
List < string > list2 = new List < string > ();
list2.Add("C");
list2.Add("D");現在,如果以下兩種返回不同的元素,那麼這意味著兩個列表不相等 −
示例
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List < string > list1 = new List < string > ();
list1.Add("P");
list1.Add("Q");
list1.Add("R");
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
Console.WriteLine("Second list...");
List < string > list2 = new List < string > ();
list2.Add("P");
list2.Add("R");
foreach(string value in list2) {
Console.WriteLine(value);
}
Console.WriteLine("Difference in the two lists...");
IEnumerable < string > list3;
list3 = list1.Except(list2);
foreach(string value in list3) {
Console.WriteLine(value);
}
}
}
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP