먼저 두 개의 목록을 설정하십시오 -
목록 1개
List < string > list1 = new List < string > ();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D"); 목록 2
List < string > list2 = new List < string > ();
list2.Add("C");
list2.Add("D"); 두 목록의 차이점을 찾고 차이점 요소를 표시하려면 -
IEnumerable < string > list3;
list3 = list1.Except(list2);
foreach(string value in list3) {
Console.WriteLine(value);
} 다음은 두 목록을 비교하는 완전한 예입니다 -
예
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List < string > list1 = new List < string > ();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
Console.WriteLine("Second list...");
List < string > list2 = new List < string > ();
list2.Add("C");
list2.Add("D");
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);
}
}
}