C# 程序,从两个或更多列表中找出公共值


创建多个列表 −

// two lists
var list1 = new List<int>{3, 4};
var list2 = new List<int>{1, 2, 3};

现在,使用 Intersect() 方法获取公共值 −

var res = 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>{3, 4};
      var list2 = new List<int>{1, 2, 3};

      // common values
      var res = list1.Intersect(list2);

      foreach(int i in res) {
         Console.WriteLine(i);
      }
   }
}

输出

3

更新日期:22-Jun-2020

316 人观看

启动你的 职业

完成课程,获得认证

开始
广告
© . All rights reserved.