C#程序打印两个列表的所有常见元素
首先创建两个列表-
List list1 = new List() {40, 20, 60, 3, 55};
List list2 = new List() {20, 70, 55, 80};要找到常见元素,请使用相交-
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("常用元素:");
         foreach(int value in list1.Intersect(list2))
         Console.WriteLine(value);
      }
   }
}输出结果
常用元素: 20 55
