当前位置: 首页 > news >正文

网站建设在开封找谁做济南哪里有网站公司

网站建设在开封找谁做,济南哪里有网站公司,wordpress 屏蔽爬虫,被代运营骗了去哪投诉在 C# 中#xff0c;要比较两个 ListT 集合的内容是否相同#xff0c;可以通过以下几种方法#xff1a; 一、非自定义类的元素比较 1. 使用 SequenceEqual 方法#xff08;顺序和内容都相等#xff09; 顺序和内容都相等#xff1a;使用 SequenceEqual。 usin… 在 C# 中要比较两个 ListT 集合的内容是否相同可以通过以下几种方法 一、非自定义类的元素比较 1. 使用 SequenceEqual 方法顺序和内容都相等 顺序和内容都相等使用 SequenceEqual。 using System; using System.Collections.Generic; using System.Linq;class Program {static void Main(){Listint list1 new Listint { 1, 2, 3, 4 };Listint list2 new Listint { 1, 2, 3, 4 };bool areEqual list1.SequenceEqual(list2);Console.WriteLine($Are the lists equal? {areEqual});} }2. 自定义比较逻辑如果顺序不重要 忽略顺序可以先对两个列表排序后再使用 SequenceEqual。 using System; using System.Collections.Generic; using System.Linq;class Program {static void Main(){Listint list1 new Listint { 1, 2, 3, 4 };Listint list2 new Listint { 4, 3, 2, 1 };bool areEqual list1.OrderBy(x x).SequenceEqual(list2.OrderBy(x x));Console.WriteLine($Are the lists equal (ignoring order)? {areEqual});} }3. 使用 Set 比较忽略重复元素 忽略重复元素可以使用 HashSetT。 using System; using System.Collections.Generic;class Program {static void Main(){Listint list1 new Listint { 1, 2, 3, 4 };Listint list2 new Listint { 4, 3, 2, 1 };bool areEqual new HashSetint(list1).SetEquals(list2);Console.WriteLine($Are the lists equal (ignoring duplicates)? {areEqual});} }二、自定义类的元素比较 如果你想比较自定义对象的集合比如 ListMyClass你需要自定义比较规则。默认情况下ListT 的比较是基于对象引用的比较即两个对象的引用是否相同而不是根据对象的内容来判断。 为了比较自定义元素你需要重写 Equals 和 GetHashCode 方法。这样比较时会依据你定义的规则进行比较。 假设你有一个自定义类 Person你想根据 Name 和 Age 属性来判断两个 Person 对象是否相同。 重写 Equals 和 GetHashCode 你需要重写 Equals 方法来比较两个对象是否相等并且重写 GetHashCode以确保集合操作如 HashSet 和 Except正常工作。 1、Equals 方法比较两个 Person 对象的 Name 和 Age 属性。 public override bool Equals(object obj){if (obj is Person other){return this.Name other.Name this.Age other.Age;}return false;} 2、GetHashCode 使用 HashCode.Combine 来生成一个基于 Name 和 Age 的哈希值确保两个内容相同的 Person 对象具有相同的哈希值。 public override int GetHashCode(){return HashCode.Combine(Name, Age);} 1. 顺序和内容都相等 顺序和内容都相等使用 SequenceEqual。 using System; using System.Collections.Generic; using System.Linq;class Person {public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return this.Name other.Name this.Age other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);} }class Program {static void Main(){ListPerson list1 new ListPerson{new Person { Name Alice, Age 25 },new Person { Name Bob, Age 30 }};ListPerson list2 new ListPerson{new Person { Name Alice, Age 25 },new Person { Name Bob, Age 30 }};bool isSame list1.SequenceEqual(list2);Console.WriteLine($顺序和内容都相等: {isSame});} }2. 忽略顺序 忽略顺序先对两个列表排序然后使用 SequenceEqual。 using System; using System.Collections.Generic; using System.Linq;class Person {public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return this.Name other.Name this.Age other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);} }class Program {static void Main(){ListPerson list1 new ListPerson{new Person { Name Alice, Age 25 },new Person { Name Bob, Age 30 }};ListPerson list2 new ListPerson{new Person { Name Bob, Age 30 },new Person { Name Alice, Age 25 }};bool isSame list1.OrderBy(p p.Name).ThenBy(p p.Age).SequenceEqual(list2.OrderBy(p p.Name).ThenBy(p p.Age));Console.WriteLine($忽略顺序: {isSame});} }3. 忽略重复元素 忽略重复元素将列表转换为 HashSetT然后使用 SetEquals 方法进行比较。  如果你希望忽略重复元素并只关心唯一元素是否相同可以使用 HashSetT 来进行比较。HashSetT 会自动去除重复元素因此可以通过将列表转换为 HashSetT 来忽略重复元素。 using System; using System.Collections.Generic;class Person {public string Name { get; set; }public int Age { get; set; }public override bool Equals(object obj){if (obj is Person other){return this.Name other.Name this.Age other.Age;}return false;}public override int GetHashCode(){return HashCode.Combine(Name, Age);} }class Program {static void Main(){ListPerson list1 new ListPerson{new Person { Name Alice, Age 25 },new Person { Name Alice, Age 25 },new Person { Name Bob, Age 30 }};ListPerson list2 new ListPerson{new Person { Name Bob, Age 30 },new Person { Name Alice, Age 25 }};bool isSame new HashSetPerson(list1).SetEquals(new HashSetPerson(list2));Console.WriteLine($忽略重复元素: {isSame});} }总结 顺序和内容都相等使用 SequenceEqual。忽略顺序可以先对两个列表排序后再使用 SequenceEqual。忽略重复元素可以使用 HashSetT。
http://www.pierceye.com/news/109258/

相关文章:

  • 美容院网站建设西安有关做网站的公司有哪些
  • 湛江做网站设计公司网络推广网站培训班
  • 大型网站建立萝岗微信网站建设
  • 门户网站建设发展趋势官网网站怎么创建
  • 企业网站如何做自然搜索phpcms 网站转移
  • 站内优化怎么做建设设计网站
  • 如何建设机器人教育网站上海有名的网络公司
  • 网站二级目录做网站淄博网站建设 很乱
  • 常州做网站企业海口网站开发建设
  • 做php网站用什么软件开发wordpress页面图片轮播图
  • 做网站页面大小多大8上的信息课做网站作业
  • 自己做微网站创欧科技 网站建设
  • 网站设计师大学学什么专业北京燕华工程建设有限公司网站
  • 广州专业网站建设企业wordpress 数据库修改
  • 淮南装饰公司网站建设seo 网站树
  • 设计素材下载网站网络营销课程设计报告
  • 安庆公司做网站搜索引擎收录提交入口
  • 城固网站建设wordpress主题谁的最好
  • 国外建站企业北京鑫创网站建设
  • 网站做视频wordpress插件手动升级
  • wordpress收不到网站没有收款接口网站怎么做收款
  • 信阳做网站推广用tornado做网站
  • 网站开发与数据库ppt成都优化网站建设
  • 毕业设计静态网站建设选题依据企业网络营销的优势
  • 网站图片动态换名网站充值支付宝收款怎么做
  • 蜘蛛网是个什么网站做特卖的购物网站
  • 从零学做网站古典 网站模板
  • h5网站模板开发网站后台哪些功能需要前端配合
  • 网站建设 宜宾佛山企业网站自助建站
  • 苏宁易购网站建设 的定位手机网站制作招聘