首先,设置两个列表 -
List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");
List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");
查找两个列表之间的差异并显示差异元素 -
IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) { Console.WriteLine(value); }
以下是比较两个列表的完整示例 -
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } Console.WriteLine("Second list..."); List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D"); foreach(string value in list2) { Console.WriteLine(value); } Console.WriteLine("Difference in the two lists..."); IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) { Console.WriteLine(value); } } }
以上就是如何在 C# 中比较两个列表并将差异添加到第三个列表?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号