================================Person.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Collections;namespaceConsoleApplication2{publicclassPerson:IComparable<Person>,IFormattable{publicdecimalMoney{get;privateset;}publicstringName{get;privateset;}publicPerson(stringname,decimalmoney){this.Name=name;this.Money=money;}publicstringToString(stringformat,IFormatProviderformatProvider){if(format==null)returnToString();switch(format){case"N":returnstring.Format("姓名:{0}",Name);case"M":returnstring.Format("收入:{0}",Money);default:returnToString();}}publicoverridestringToString(){returnstring.Format("姓名:{0},收入:{1}",Name,Money);}publicintCompareTo(Personother){intresouce=other.Money.CompareTo(this.Money);//降序if(resouce==0){resouce=other.Name.CompareTo(this.Name);}returnresouce;}}}

================================Student.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication2{[Serializable]//指示一个类可以序列化,不可以继承publicclassStudent{publicstringName{get;set;}publicStudent(stringname){this.Name=name;}publicoverridestringToString(){returnstring.Format("我是学生了,姓名:{0}",this.Name);}}}

================================主程序

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication2{classProgram{staticvoidMain(string[]args){//=============================集合初始值设定项List<int>i=newList<int>(){1,2};List<string>s=newList<string>(){"a","b"};//=============================添加元素Personp1=newPerson("张飞",100.00m);Personp2=newPerson("关羽",150.00m);List<Person>p=newList<Person>(){p1,p2};//集合初始值设定项p.Add(newPerson("诸葛亮",500.00m));p.AddRange(newPerson[]{newPerson("赵云",50.00m),newPerson("马超",100.00m)});//添加集合List<Person>per=newList<Person>(newPerson[]{newPerson("赵云",50.00m),newPerson("马超",100.00m)});//构造对象//=============================插入元素p.Insert(1,newPerson("黄忠",100.00m));p.InsertRange(0,newPerson[]{newPerson("魏延",80.00m),newPerson("庞统",100.00m)});//批量插入p.InsertRange(0,newPerson[]{newPerson("魏延",80.00m),newPerson("庞统",100.00m)});//批量插入//=============================删除元素//p.Remove(p1);//按对象删除//p.RemoveAt(0);//按索引删除p.RemoveRange(0,1);//第一个参数开始索引位置;第二个参数删除的个数//=============================查找元素Console.WriteLine(p.Exists(r=>r.Name=="张飞"));//找到返回True,否则返回falseConsole.WriteLine(p.IndexOf(p1));//根据对象查找,【找到为1,没找到为-1】Console.WriteLine(p.LastIndexOf(p1));//根据对象查找,从后往前查Console.WriteLine(p.FindIndex(newFindPreson("庞统").Equals));//根据姓名查找,【返回索引】Console.WriteLine(p.FindIndex(r=>r.Name=="庞统"));//FindIndexlambda方式Console.WriteLine(p.FindLastIndex(r=>r.Name=="庞统"));//和FindIndex相似,从最后一个往前找Console.WriteLine(p.Find(r=>r.Name=="庞统").ToString());//根据姓名查找,【返回一个对象】Console.WriteLine(p.FindAll(r=>r.Name=="庞统")[0].ToString());//根据姓名查找,返回所有匹配对象Console.WriteLine("==============");//=============================排序p.Sort();//没有参数的sort,person必须实现IComparable<T>接口,倒序p.Sort(newSortPerson(PersonType.Money));//倒序p.Sort((x,y)=>x.Money.CompareTo(y.Money));//正序//=============================访问元素(for,foreach)//p.ForEach(Console.WriteLine);//委托Console.WriteLine显式每一项的值p.ForEach(r=>Console.WriteLine(r));//使用lambda表达式//foreach(variteminp)//{//Console.WriteLine(item.ToString());//}//=============================强制转换List<Student>stu=p.ConvertAll(r=>newStudent(r.Name));stu.ForEach(Console.WriteLine);Console.ReadKey();}privatestaticboolHandleAction(Personp){Console.WriteLine(p.Name);returntrue;}}//用于比较classFindPreson:IEquatable<Person>{privatestringName;publicFindPreson(stringname){this.Name=name;}publicoverrideintGetHashCode(){returnbase.GetHashCode();}publicoverrideboolEquals(objectobj){if(obj==null)thrownewArgumentException("对象不能为空");returnEquals(objasPerson);}publicboolEquals(Personother){if(other==null)thrownewArgumentException("对象不能为空");returnthis.Name==other.Name;}}//用于排序enumPersonType{Name,Money}classSortPerson:IComparer<Person>{PersonTypept;publicSortPerson(PersonTypep){pt=p;}publicintCompare(Personx,Persony){if(x==null||y==null)thrownewArgumentException("对象不能为null");intsource=0;switch(pt){casePersonType.Money:source=y.Money.CompareTo(x.Money);//降序break;casePersonType.Name:source=y.Name.CompareTo(x.Name);//降序break;default:source=y.Money.CompareTo(x.Money);//降序break;}returnsource;}}}