我是靠谱客的博主 殷勤野狼,这篇文章主要介绍C#中关于List<T>的并集与交集以及差集解析,现在分享给大家,希望可以做个参考。

集合的并集是合并集合的项,如下图所示:


复制代码
1
2
3
4
5
6
7
List<int> ls1 = new List<int>() { 1,2,3,5,7,9 }; List<int> ls2 = new List<int>() { 2,4,6,8,9,10}; IEnumerable<int> unionLs = ls1.Union(ls2);foreach (int item in unionLs) { Console.Write("{0}t", item); }
登录后复制

集合的交集是取集合的共同的项,如下图所示:


复制代码
1
2
3
4
5
6
7
8
List<int> ls1 = new List<int>() { 1,2,3,5,7,9 }; List<int> ls2 = new List<int>() { 2,4,6,8,9,10}; IEnumerable<int> intersectLs = ls1.Intersect(ls2);foreach (int item in intersectLs) { Console.Write("{0}t",item); }
登录后复制

集合的差集是取在该集合中而不在另一集合中的所有的项,如下图所示:


复制代码
1
2
3
4
5
6
7
8
List<int> ls1 = new List<int>() { 1,2,3,5,7,9 }; List<int> ls2 = new List<int>() { 2,4,6,8,9,10}; IEnumerable<int> exceptLs = ls1.Except(ls2);foreach (int item in exceptLs) { Console.Write("{0}t", item); }
登录后复制

以上就是C#中关于List<T>的并集与交集以及差集解析的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是殷勤野狼最近收集整理的关于C#中关于List<T>的并集与交集以及差集解析的全部内容,更多相关C#中关于List<T>内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部