设置两个列表 -
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");
现在,如果以下返回不同的元素,则意味着列表不相等 -
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("P"); list1.Add("Q"); list1.Add("R"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } Console.WriteLine("Second list..."); List < string > list2 = new List < string > (); list2.Add("P"); list2.Add("R"); 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号