溫馨提示×

C#中怎么找到兩個集合的交集

c#
小億
219
2024-04-02 09:45:56
欄目: 編程語言

在C#中,可以使用LINQ(Language-Integrated Query)來找到兩個集合的交集。下面是一個示例代碼:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
        List<int> list2 = new List<int> { 3, 4, 5, 6, 7 };

        var intersection = list1.Intersect(list2);

        Console.WriteLine("Intersection of the two lists:");
        foreach (var item in intersection)
        {
            Console.WriteLine(item);
        }
    }
}

在上面的示例中,我們首先創建了兩個整數類型的列表list1和list2,并使用Intersect方法找到它們的交集。最后,使用foreach循環輸出交集中的元素。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女