================================Person.cs
创新互联是专业的宝安网站建设公司,宝安接单;提供做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行宝安网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication2 { public class Person : IComparable, IFormattable { public decimal Money { get; private set; } public string Name { get; private set; } public Person(string name,decimal money ) { this.Name = name; this.Money = money; } public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(); switch (format) { case "N": return string.Format("姓名:{0}", Name); case "M": return string.Format("收入:{0}", Money); default: return ToString(); } } public override string ToString() { return string.Format("姓名:{0},收入:{1}", Name, Money); } public int CompareTo(Person other) { int resouce = other.Money.CompareTo(this.Money);//降序 if (resouce == 0) { resouce = other.Name.CompareTo(this.Name); } return resouce; } } }
================================Student.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { [Serializable]//指示一个类可以序列化,不可以继承 public class Student { public string Name { get; set; } public Student(string name) { this.Name = name; } public override string ToString() { return string.Format("我是学生了,姓名:{0}", this.Name); } } }
================================主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //=============================集合初始值设定项 Listi = new List () { 1, 2 }; List s = new List () { "a", "b" }; //=============================添加元素 Person p1 = new Person("张飞", 100.00m); Person p2 = new Person("关羽", 150.00m); List p = new List () { p1, p2 };//集合初始值设定项 p.Add(new Person("诸葛亮", 500.00m)); p.AddRange(new Person[] { new Person("赵云", 50.00m), new Person("马超", 100.00m) });//添加集合 List per = new List (new Person[] { new Person("赵云", 50.00m), new Person("马超", 100.00m) });//构造对象 //=============================插入元素 p.Insert(1, new Person("黄忠", 100.00m)); p.InsertRange(0, new Person[] { new Person("魏延", 80.00m), new Person("庞统", 100.00m) });//批量插入 p.InsertRange(0, new Person[] { new Person("魏延", 80.00m), new Person("庞统", 100.00m) });//批量插入 //=============================删除元素 //p.Remove(p1);//按对象删除 //p.RemoveAt(0);//按索引删除 p.RemoveRange(0, 1);//第一个参数开始索引位置;第二个参数删除的个数 //=============================查找元素 Console.WriteLine(p.Exists(r => r.Name == "张飞"));//找到返回True,否则返回false Console.WriteLine(p.IndexOf(p1)); //根据对象查找,【找到为1,没找到为-1】 Console.WriteLine(p.LastIndexOf(p1));//根据对象查找,从后往前查 Console.WriteLine(p.FindIndex(new FindPreson("庞统").Equals));//根据姓名查找,【返回索引】 Console.WriteLine(p.FindIndex(r => r.Name == "庞统"));//FindIndex lambda方式 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 接口,倒序 p.Sort(new SortPerson(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 (var item in p) //{ // Console.WriteLine(item.ToString()); //} //=============================强制转换 List stu = p.ConvertAll(r => new Student(r.Name)); stu.ForEach(Console.WriteLine); Console.ReadKey(); } private static bool HandleAction(Person p) { Console.WriteLine(p.Name); return true; } } //用于比较 class FindPreson : IEquatable { private string Name; public FindPreson(string name) { this.Name = name; } public override int GetHashCode() { return base.GetHashCode(); } public override bool Equals(object obj) { if (obj == null) throw new ArgumentException("对象不能为空"); return Equals(obj as Person); } public bool Equals(Person other) { if (other == null) throw new ArgumentException("对象不能为空"); return this.Name == other.Name; } } //用于排序 enum PersonType { Name, Money } class SortPerson:IComparer { PersonType pt; public SortPerson(PersonType p) { pt = p; } public int Compare(Person x, Person y) { if (x == null || y == null) throw new ArgumentException("对象不能为null"); int source = 0; switch (pt) { case PersonType.Money: source = y.Money.CompareTo(x.Money);//降序 break; case PersonType.Name: source = y.Name.CompareTo(x.Name);//降序 break; default: source = y.Money.CompareTo(x.Money);//降序 break; } return source; } } }