首页主机资讯c# intersect可行吗

c# intersect可行吗

时间2025-09-26 22:47:03发布访客分类主机资讯浏览722
导读:在C#中,Intersect方法用于获取两个集合的交集 using System; using System.Collections.Generic; class Program { static void Main( ...

在C#中,Intersect方法用于获取两个集合的交集

using System;
    
using System.Collections.Generic;


class Program
{

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

        var intersection = list1.Intersect(list2);
    

        Console.WriteLine("Intersection: ");

        foreach (var item in intersection)
        {
    
            Console.WriteLine(item);

        }

    }

}
    

在这个示例中,我们创建了两个整数列表list1list2,然后使用Intersect方法找到它们的交集。最后,我们遍历并输出交集中的元素。

请注意,Intersect方法返回一个新的集合,其中包含两个输入集合的公共元素。原始集合不会受到影响。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: c# intersect可行吗
本文地址: https://pptw.com/jishu/709133.html
c# intersect有几种 c# intersect有效果吗

游客 回复需填写必要信息