卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

如何在 C# 中比较两个列表是否相等?

如何在 C# 中比较两个列表是否相等?

设置两个列表 -

列表一个

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);
      }
   }
}

卓越飞翔博客
上一篇: 解决常见的PHP Parse error: syntax error, unexpected T_VARIABLE错误
下一篇: 如何在Python中将字符串转换为数字?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏