检查 SortedSet 和指定集合是否包含相同的元素(在 C# 中)


为了检查 SortedSet 和指定集合是否包含相同的元素,请用以下代码 −

示例

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(100);
      set1.Add(200);
      set1.Add(300);
      SortedSet<int> set2 = new SortedSet<int>();
      set2.Add(450);
      set2.Add(550);
      set2.Add(650);
      set2.Add(750);
      set2.Add(800);
      Console.WriteLine("Does it contain the same elements? = "+set1.SetEquals(set2));
   }
}

输出

此代码将产生以下输出 −

Does it contain the same elements? = False

Learn C# in-depth with real-world projects through our C# certification course. Enroll and become a certified expert to boost your career.

示例

我们来看另一个示例 −

 实时演示

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(100);
      set1.Add(200);
      set1.Add(300);
      SortedSet<int> set2 = new SortedSet<int>();
      set2.Add(100);
      set2.Add(200);
      set2.Add(300);
      Console.WriteLine("Does it contain the same elements? = "+set1.SetEquals(set2));
   }
}

输出

此代码将产生以下输出 −

Does it contain the same elements? = True

更新时间: 2019 年 12 月 6 日

77 次浏览

开启您的 职业生涯

完成课程后获得认证

开始
广告