C#List常用方法及注意事项.docx_第1页
C#List常用方法及注意事项.docx_第2页
C#List常用方法及注意事项.docx_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C# 中List的常用方法List A : 1 , 2 , 3 , 5 , 9 List B : 4 , 3 , 9 1.取交集 (A和B都有)var intersectedList = list1.Intersect(list2);结果 : 3 , 9 2.取差集 (A有,B沒有) var expectedList = list1.Except(list2);结果 : 1 , 2 , 5 3.取并集 (包含A和B) var unionList =list1.Union(list2);结果 : 1 , 2 , 3 , 4 , 5 , 9 4.合并 (A+B)var unionList =list1.Concat(list2);结果 : 1 , 2 , 3 , 5 , 9 ,4 ,3 ,9 5.是否存在符合条件的值ListA.Exist(i=i3)结果 : true6. 符合条件的值的个数ListA.Count(i=i3)结果 : 27.是否所有值都符合条件ListA.TrueForAll(i=i0)结果 : true8. Find 返回第一个符合条件的值 FindAll返回所有符合条件的值 FindLast返回最后符合条件的值 FindIndex返回第一个符合条件的值下标 FindLastIndex返回最后第一个符合条件的值下标9. ConvertAll将当前 List 中的元素转换为另一种类型,并返回包含转换后的元素的列表10. Skip 跳过前面指定数量的值返回后面的值 SkipWhile跳过所有符合条件的值返回剩下的值注意:取交,差,并集以及进行比较时,要注意List中数据是值类型还是引用类型,可以定义数据类型继承IEqualityComparer接口以方便比较和筛选例: public class Student:IEqualityComparer private string m_Name; public string Name get return m_Name; set m_Name = value; private int m_Age; public int Age get return m_Age; set m_Age = value; public bool Equals(Student x, Student y) /比较x和y对象是否相同 return ( x.Name = y.Name & x.Age=y.Age); public int GetHashCode(Student obj) return obj.ToString().GetHashCode(); 然后再进行Distinct 或 Contains 或 取交,并,差集操作,使用方式: list1.Add(new StudentName=test1,Age=3); list1.Add(new StudentName=test2,Age=4); list1.Add(new StudentName=test1,Age=3); list1.Add(new StudentName=test2,Age=4); list1 = list1.Distinct(new Student ().ToList();也可以单写写为一个类例: public class Student private string m_Name; public string Name get return m_Name; set m_Name = value; private int m_Age; public int Age get return m_Age; set m_Age = value; public class StudentComparre:IEqualityComparer public bool Equals(Student x, Student y) /比较x和y对象是否相同,按照地址比较 return ( x.Name = y.Name & x.Age=y.Age); public int GetHashCode(Student obj) return

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论