공통 요소를 얻기 위해 Intesect 방법을 사용하십시오 -
목록 만들기 -
var list1 = new List{99, 87};
var list2 = new List{56, 87, 45, 99}; 이제 Intersect() 메서드를 사용하여 위 목록에서 공통 요소를 가져옵니다. -
list1.Intersect(list2);
다음은 전체 코드입니다 -
예시
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
// two lists
var list1 = new List<int>{99, 87};
var list2 = new List<int>{56, 87, 45, 99};
// common values
var res = list1.Intersect(list2);
foreach(int i in res) {
Console.WriteLine(i);
}
}
} 출력
99 87