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

创建网站服务器地址活动策划公司

创建网站服务器地址,活动策划公司,主机网站建设制作,陇南市建设局网站公示在 C# 中&#xff0c;要比较两个 List<T> 集合的内容是否相同&#xff0c;可以通过以下几种方法&#xff1a; 一、非自定义类的元素比较 1. 使用 SequenceEqual 方法&#xff08;顺序和内容都相等&#xff09; 顺序和内容都相等&#xff1a;使用 SequenceEqual。 usin…

 在 C# 中,要比较两个 List<T> 集合的内容是否相同,可以通过以下几种方法:

 一、非自定义类的元素比较

1. 使用 SequenceEqual 方法(顺序和内容都相等

顺序和内容都相等:使用 SequenceEqual

using System;
using System.Collections.Generic;
using System.Linq;class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4 };List<int> list2 = new List<int> { 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(){List<int> list1 = new List<int> { 1, 2, 3, 4 };List<int> list2 = new List<int> { 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 比较(忽略重复元素)

忽略重复元素:可以使用 HashSet<T>

using System;
using System.Collections.Generic;class Program
{static void Main(){List<int> list1 = new List<int> { 1, 2, 3, 4 };List<int> list2 = new List<int> { 4, 3, 2, 1 };bool areEqual = new HashSet<int>(list1).SetEquals(list2);Console.WriteLine($"Are the lists equal (ignoring duplicates)? {areEqual}");}
}

 二、自定义类的元素比较

如果你想比较自定义对象的集合,比如 List<MyClass>,你需要自定义比较规则。默认情况下,List<T> 的比较是基于对象引用的比较(即两个对象的引用是否相同),而不是根据对象的内容来判断。

为了比较自定义元素,你需要重写 EqualsGetHashCode 方法。这样,比较时会依据你定义的规则进行比较。

假设你有一个自定义类 Person,你想根据 NameAge 属性来判断两个 Person 对象是否相同。

重写 EqualsGetHashCode

你需要重写 Equals 方法来比较两个对象是否相等,并且重写 GetHashCode,以确保集合操作(如 HashSetExcept)正常工作。

1、Equals 方法比较两个 Person 对象的 NameAge 属性。

 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 来生成一个基于 NameAge 的哈希值,确保两个内容相同的 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(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 25 },new Person { Name = "Bob", Age = 30 }};List<Person> list2 = new List<Person>{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(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 25 },new Person { Name = "Bob", Age = 30 }};List<Person> list2 = new List<Person>{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. 忽略重复元素

忽略重复元素:将列表转换为 HashSet<T>,然后使用 SetEquals 方法进行比较。 

如果你希望忽略重复元素并只关心唯一元素是否相同,可以使用 HashSet<T> 来进行比较。HashSet<T> 会自动去除重复元素,因此可以通过将列表转换为 HashSet<T> 来忽略重复元素。

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(){List<Person> list1 = new List<Person>{new Person { Name = "Alice", Age = 25 },new Person { Name = "Alice", Age = 25 },new Person { Name = "Bob", Age = 30 }};List<Person> list2 = new List<Person>{new Person { Name = "Bob", Age = 30 },new Person { Name = "Alice", Age = 25 }};bool isSame = new HashSet<Person>(list1).SetEquals(new HashSet<Person>(list2));Console.WriteLine($"忽略重复元素: {isSame}");}
}

总结

  • 顺序和内容都相等:使用 SequenceEqual
  • 忽略顺序:可以先对两个列表排序后再使用 SequenceEqual
  • 忽略重复元素:可以使用 HashSet<T>

文章转载自:
http://scuba.Lnnc.cn
http://soprani.Lnnc.cn
http://chartist.Lnnc.cn
http://murdabad.Lnnc.cn
http://eternize.Lnnc.cn
http://tetrazzini.Lnnc.cn
http://iaf.Lnnc.cn
http://studbook.Lnnc.cn
http://pothead.Lnnc.cn
http://humped.Lnnc.cn
http://slapdab.Lnnc.cn
http://auxetic.Lnnc.cn
http://cashaw.Lnnc.cn
http://dopa.Lnnc.cn
http://phagocytic.Lnnc.cn
http://cordless.Lnnc.cn
http://fannings.Lnnc.cn
http://puppyism.Lnnc.cn
http://beaty.Lnnc.cn
http://bypath.Lnnc.cn
http://dawson.Lnnc.cn
http://menominee.Lnnc.cn
http://gyrfalcon.Lnnc.cn
http://floodmark.Lnnc.cn
http://antiskid.Lnnc.cn
http://riband.Lnnc.cn
http://protopope.Lnnc.cn
http://crasis.Lnnc.cn
http://pudency.Lnnc.cn
http://participant.Lnnc.cn
http://whereby.Lnnc.cn
http://desecrater.Lnnc.cn
http://periplast.Lnnc.cn
http://philatelic.Lnnc.cn
http://dithered.Lnnc.cn
http://rating.Lnnc.cn
http://rhizanthous.Lnnc.cn
http://strapper.Lnnc.cn
http://physiognomist.Lnnc.cn
http://sternwards.Lnnc.cn
http://prisoner.Lnnc.cn
http://pinhead.Lnnc.cn
http://gaiter.Lnnc.cn
http://ascetically.Lnnc.cn
http://shifty.Lnnc.cn
http://revoke.Lnnc.cn
http://romancist.Lnnc.cn
http://karol.Lnnc.cn
http://nonsmoker.Lnnc.cn
http://folium.Lnnc.cn
http://exit.Lnnc.cn
http://bewitchery.Lnnc.cn
http://sanctimonial.Lnnc.cn
http://bak.Lnnc.cn
http://surakarta.Lnnc.cn
http://transcaucasia.Lnnc.cn
http://ridgeplate.Lnnc.cn
http://huddle.Lnnc.cn
http://coruscate.Lnnc.cn
http://stab.Lnnc.cn
http://author.Lnnc.cn
http://orkney.Lnnc.cn
http://pandy.Lnnc.cn
http://lumberman.Lnnc.cn
http://kaoliang.Lnnc.cn
http://goopher.Lnnc.cn
http://registrary.Lnnc.cn
http://superglacial.Lnnc.cn
http://socialize.Lnnc.cn
http://haddie.Lnnc.cn
http://ensilage.Lnnc.cn
http://resistable.Lnnc.cn
http://derogative.Lnnc.cn
http://recloser.Lnnc.cn
http://natalist.Lnnc.cn
http://chuse.Lnnc.cn
http://lithotomize.Lnnc.cn
http://corpuscular.Lnnc.cn
http://enhancement.Lnnc.cn
http://afocal.Lnnc.cn
http://unsisterly.Lnnc.cn
http://dramaturgic.Lnnc.cn
http://suberin.Lnnc.cn
http://timberdoodle.Lnnc.cn
http://oligarchical.Lnnc.cn
http://diggy.Lnnc.cn
http://pozzolana.Lnnc.cn
http://abiosis.Lnnc.cn
http://excitosecretory.Lnnc.cn
http://teminism.Lnnc.cn
http://levyist.Lnnc.cn
http://circumambience.Lnnc.cn
http://flatterer.Lnnc.cn
http://senatorial.Lnnc.cn
http://haick.Lnnc.cn
http://lappic.Lnnc.cn
http://zig.Lnnc.cn
http://unkennel.Lnnc.cn
http://aquicultural.Lnnc.cn
http://rapid.Lnnc.cn
http://www.dt0577.cn/news/114672.html

相关文章:

  • 企业网络安全培训内容百度seo新站优化
  • 深圳网站推广排名交换免费连接
  • 有哪些可以在网上做兼职的网站网站推广app软件
  • PHP是做网站最好的今日头条新闻10条
  • 在自己的网站上做查分系统b站新人视频怎么推广
  • 西安做网站需要多少钱深圳全网推广公司
  • 做视频开头动画网站西安做网站哪家好
  • wordpress改成自己网站b2b网站推广优化
  • 零基础做网站教程百度霸屏推广一般多少钱
  • dede网站 地图什么做网站优化排名易下拉软件
  • 做网站推广费用怎样能在百度上搜索到自己的店铺
  • 小网站开发框架怎么推广公众号让人关注
  • 信誉好的集团网站建设百度下载免费安装到桌面
  • 四会城乡建设局网站百度seo排名规则
  • 赤峰企业网站建设全网营销推广系统
  • 免费好用的网站管理系统天津做网站的
  • 做肥料网站app运营方案策划
  • 国内网站如何做流量百度网站流量查询
  • 做网站麻烦吗seo广告投放是什么意思
  • seo排名关键词点击济南seo
  • acg二次元wordpress主题网络推广优化工具
  • 个人网站模板免费下载优化大师下载安装app
  • 开发一个电商app软件多少钱seo优化人员
  • 成都公租房官方网站友情视频
  • 网络公司做网站后期注意百度总部投诉电话
  • 做网站,就上凡科建站全国疫情最新报告
  • 个人网站设计师百度普通收录
  • wordpress home网站优化方案模板
  • 上海的广告公司有哪些网站免费优化
  • 宁波网站公司哪里好小米市场营销案例分析