먼저 두 개의 목록을 만듭니다 -
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80}; 공통 요소를 찾으려면 Intersect −
를 사용하세요.list1.Intersect(list2)
다음은 두 목록 사이의 공통 요소를 찾는 완전한 코드입니다 -
예시
using System;
using System.Linq;
using System.Collections.Generic;
namespace Demo {
class Program {
static void Main(string[] args) {
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80};
Console.WriteLine("Common elements:");
foreach(int value in list1.Intersect(list2))
Console.WriteLine(value);
}
}
} 출력
Common elements: 20 55